Join and Include in Entity Framework

后端 未结 1 1540
鱼传尺愫
鱼传尺愫 2020-11-30 09:56

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

相关标签:
1条回答
  • 2020-11-30 10:05

    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 => 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.

    0 讨论(0)
提交回复
热议问题