IQueryable< T> .Include()被忽略 [英] IQueryable<T>.Include() gets ignored

查看:85
本文介绍了IQueryable< T> .Include()被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父母 Child 相互关联的实体为1到M.我需要一起查询孩子与单一SQL查询中的父母,但 Include 方法在某些情况下无法正常工作。

这是一个正确的单一查询, code> Parent 和 Child tables(via JOIN):

I have Parent and Child entities related to each other as 1 to M. I need to query childs together with parents within a single SQL query, but the Include method is not properly working for some cases.
This one makes a correct single query for both Parent and Child tables (via JOIN):

var r1 =
    ctx.Set<Parent>()
       .Include(p => p.Childs)
       .Where(p => p.Id == 1)
       .ToList();

一旦我立即创建一个匿名对象,孩子们会迷路,SQL只包含Parent的字段。孩子的检索仍然很懒 - 他们还没有加载:

Once i create an anonymous object on the fly, children are getting lost and SQL contains only Parent's fields. Retrieval of children remains lazy - they are still not loaded:

var r2 =
    ctx.Set<Parent>()
       .Include(p => p.Childs)
       .Where(p => p.Id == 2)
       .Select(p => new { myParent = p})
       .ToList();

问题:


    为什么是这样?
  1. 如何在LINQ中构建一个新的匿名对象,这样孩子们不会丢失?

p.s。我想保留Parent的虚拟属性。

p.s. i'd like keep Childs property of Parent virtual.

推荐答案

这是所有EF版本中的一般问题。 EF尽可能地通过包含,但是当查询的形状发生变化时,包含将不可逆转地丢失。

This is a general problem in all versions of EF known to me. EF tries hard to pass the 'includes' as far as possible, but when "the shape of the query changes", the 'includes' are irreversibly lost.

形状的查询更改例如:


  • 使用投影(选择不是整个对象,只是一些字段,或不同的对象)

  • 使用group-by或其他聚合

  • ..可能还有更多的情况,目前我不记得了。 >
  • a projection is used (select not whole object but just some fields, or different object)
  • a group-by or other aggregation is used
  • .. and probably in some more cases, which currently I dont remember.

令人遗憾的是,我也不记得MSDN上的哪些地方我偶然发现了查询形状的解释。如果我找到它,我会在这里留下一个链接。

Sadly, I also dont remember where on MSDN I stumbled upon the "shape of the query" explanation. If I find it, I'll drop here a link.

解决方案其实很简单:只是简单地指定'include'部分,但最终结果。所以,而不是开始时 set.include(x),请执行 .Select(.. => new {..,x})手动包含'x'。它也可以在分组期间工作,因为您也可以在那里进行投影。

The solution is actually quite simple: simply specify the 'include' part not early, but at the final result. So, instead of set.include(x) at beginning, do .Select( .. => new { .., x }) to include the 'x' manually. It also works during grouping, as you can do a projection there too.

但是,这不是一个解决方案。这是一个手动补丁/修补程序,它不解决任何问题。考虑到您可能希望通过某些界面公开IQueryable,您可能希望公开一些已经包含的基本查询。当然,这种做法一般都不可能,就像接口的客户端做一个投影或分组一样,他会失去包含,他甚至不知道哪个应该是是。对我来说,这是EF的一个主要缺陷。

However, this is not a solution. This is a manual patch/hotfix, which does not solve anything. Considering that you might want to expose a "IQueryable<>" through some interface, you may like to expose a "base query" with some things already .Included. And of course, this is simply not possible to do in a general way, as if the client of the interface does a projection or grouping, he will lose the includes and he will not even know which ones should be. For me, this is a major deficiency in EF.

编辑:刚找到一个:。包含在以下查询中不包括真的不是MSDN,但是一样好。

just found one: .Include in following query does not include really Not MSDN, but just as good.

这篇关于IQueryable&lt; T&gt; .Include()被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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