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

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

问题描述

我有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;

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

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

下面的一个很好的,免费的书LINQ

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

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