加入并包含在实体框架中 [英] Join and Include in Entity Framework

查看:88
本文介绍了加入并包含在实体框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下linq对实体的查询。问题在于,即使我已经包含了一个东西,似乎并没有加载标签关系。如果我不加入标签,我可以正常工作。

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

有什么其他方式要问这个查询?可能要分开或是某些东西?

解决方案

Include说,加载所有标签。在那里说,加载一些标签。当查询和Include之间存在矛盾时,查询将永远胜出。



要从任何标签返回 strong> item with 至少一个标签== text:

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

(未经验证,因为我没有您的DB /型号)



这里是一本非常好的免费在LINQ上的书


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?

解决方案

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.

这篇关于加入并包含在实体框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆