Entity Framework Join and Include

.net c# entity-framework linq sql

Question

I have the linq to entities query shown below. The issue is that, despite the fact that I added an item for the "Tags" connection, it doesn't appear to load. If I do not join on tags, it functions perfectly, but I must.

            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 an other way to phrase this question? Perhaps divide it up or something?

1
29
7/29/2012 7:48:00 AM

Accepted Answer

The Include, however, conflicts with the where. It reads, "Load all tags," in Include. Load some tags, it says there. When the query and Include are in conflict, the query will always prevail.

Returning one tag with all tags from an item with any tags == text

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

(Untested since I need your DB/model)

I give you a great, unpaid guide to LINQ.

50
1/9/2009 1:54:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow