C# 加入并包含在实体框架中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/416847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Join and Include in Entity Framework
提问by Olaj
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.
我有以下 linq to entity 查询。问题是它似乎没有加载“标签”关系,即使我已经为它包含了一个东西。如果我不加入标签,它工作正常,但我需要这样做。
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?
有没有其他方法可以询问这个查询?也许拆分它或什么?
采纳答案by Craig Stuntz
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.
好吧, Include 与 where 相矛盾。Include 说,“加载所有标签。” where 说,“加载一些标签。” 当查询和Include 之间存在矛盾时,查询将永远获胜。
To return alltags from anyitem with at leastone 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.