Entity Framework 4.1 Code First - 使用 LinqKit PredicateBuilder 时包含被忽略

发布于 2024-11-16 15:26:42 字数 809 浏览 0 评论 0原文

我正在使用 Entity Framework 4.1 Code First,并且还使用 PredicateBuilder,以便我可以跨多个规范类构建谓词表达式(使用规范模式)。我能够正确构建谓词并将其应用到 DbSet,并且我得到的数据正是我所期望的。但是,无论我尝试什么,它总是延迟加载。这是我如何构建谓词并应用它的简单示例。

IQueryable<HairColor> hairColorQuery = Context.Set<HairColor>().AsExpandable();

Expression<Func<Parent, bool>> parentPredicate = PredicateBuilder.And(PredicateBuilder.True<Parent>(), p => p.NameLast.StartsWith("V")).Expand();

Expression<Func<HairColor, bool>> hairColorPredicate = PredicateBuilder.And(PredicateBuilder.True<HairColor>(), h => h.Parents.AsQueryable().Any(parentPredicate));

HairColor[] hairColors = hairColorQuery.Where(hairColorPredicate).Include(h => h.Parents).ToArray();

就像我上面说的,我正在获取我想要的数据,但它忽略了 Include。

有人有什么想法吗?

I am using Entity Framework 4.1 Code First and am also using the PredicateBuilder so that I can build predicate expressions across multiple Specification classes (using the Specification pattern). I am able to properly build a predicate and apply it to a DbSet, and the data I get is what I expect. However, no matter what I try, it's always lazy loading. This is a simple example of how I'm building the predicate and applying it.

IQueryable<HairColor> hairColorQuery = Context.Set<HairColor>().AsExpandable();

Expression<Func<Parent, bool>> parentPredicate = PredicateBuilder.And(PredicateBuilder.True<Parent>(), p => p.NameLast.StartsWith("V")).Expand();

Expression<Func<HairColor, bool>> hairColorPredicate = PredicateBuilder.And(PredicateBuilder.True<HairColor>(), h => h.Parents.AsQueryable().Any(parentPredicate));

HairColor[] hairColors = hairColorQuery.Where(hairColorPredicate).Include(h => h.Parents).ToArray();

Like I said above, I'm getting the data back that I want, but it ignores the Include.

Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-11-23 15:26:42

您好,很晚才看到这一点,但在使用 LinqKits 谓词生成器时使用扩展 Include 方法时遇到了同样的问题。上一个答案中提到的问题是将 LinqKits ExpandableQuery 转换为 ObjectQuery(根据 Include 扩展的要求)会导致 null。

然而发现这个链接 http://petemontgomery.wordpress.com/2011/02/10/a -universal-predicatebuilder/ 这是一个谓词构建器,它不使用 AsExpandable 来执行搜索,因此可以在其上使用 Include 方法。尽管上面的解决方案也适用于我,但是这个不同的谓词构建器使我能够保持代码更干净/更一致

Hi jumping in late on this, but had the same issue with using an extension Include method when using LinqKits predicate builder. The problem as referred to in the previous answer is that casting LinqKits ExpandableQuery to ObjectQuery (as required by the Include extension) results in null.

However found this link http://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/ which is a Predicate builder which doesn't use AsExpandable to perform the search and hence the Include method can be used on it. Although the solution above also worked for me this different predicate builder allowed me to keep my code cleaner/more consistent

_畞蕅 2024-11-23 15:26:42

可能是 更改查询形状。尝试这个解决方法

HairColor[] hairColors = hairColorQuery.Where(hairColorPredicate)
                                       .Select(hc => new 
                                                     {
                                                         HairColor = hc,
                                                         Parents = hc.Parents // eager load
                                                     })
                                       .AsEnumerable()
                                       .Select(hc => hc.HairColor);

It's probably changing the query shape. Try this workaround

HairColor[] hairColors = hairColorQuery.Where(hairColorPredicate)
                                       .Select(hc => new 
                                                     {
                                                         HairColor = hc,
                                                         Parents = hc.Parents // eager load
                                                     })
                                       .AsEnumerable()
                                       .Select(hc => hc.HairColor);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文