Title: Join and Include in Entity Framework
Tags: c#;.net;sql;linq;entity-framework
Question: I have the following query of linq to entities. The problem is that it doesn't seem to load the "Tags" relation even though i have included a thing for it. It works fine if i do not join on tags but i need to do that.

```            var items = from i in db.Items.Include("Tags")
                        from t in i.Tags
                        where t.Text == text
                        orderby i.CreatedDate descending
                        select i;
```

Is there any other way to ask this query? Maybe split it up or something?


Here is the accepted answer: Well, the Include contradicts the where. Include says, "Load all tags." The where says, "Load some tags." When there is a contradiction between the query and Include, the query will always win.

To return all tags from any item with at least one tag == text:

```        var items = from i in db.Items.Include("Tags")
                    where i.Tags.Any(t =&gt; t.Text == text)
                    orderby i.CreatedDate descending
                    select i;
```

(Untested, as I don't have your DB/model)

Here's a really good, free book on LINQ.

Comment for this answer: Any suggestions on how to write it in some other way so i can get the tags and do a condition on them. It would be kind of simple in regular SQL.
Comment for this answer: Do you want to include ALL tags from ANY item with AT LEAST one tag == text?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2009:00:01/06/09', 'FavoriteCount': 4.0, 'PostTypeId': 1, 'Tags': '<c#><.net><sql><linq><entity-framework>', 'Title': 'Join and Include in Entity Framework', 'ViewCount': 62184, 'id': 416847, 'user_AboutMe': '', 'user_AccountId': 17918.0, 'user_DisplayName': 'Olaj', 'user_Reputation': 1742.0, 'user_Views': 271.0, 'user_id': 40609.0}}