加入和包含在实体框架实体、框架

由网友(15.浅笑)分享简介:我有LINQ的下面的查询实体。现在的问题是,它似乎没有加载了标签的关系,即使我已经包括了这事。它工作正常,如果我不加入就标签,但我需要做的。I have the following query of linq to entities. The problem is that it doesn't seem to lo...

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

阅读全文

相关推荐

最新文章