如果我从 IQueryable 中选择,则包含丢失 [英] If I select from an IQueryable then the Include is lost
问题描述
在我对 IQueryable
查询执行选择后,include
不起作用.有没有解决的办法?我的查询是
The include
does not work after I perform a select on the IQueryable
query.
Is there a way around this? My query is
public IQueryable<Network> GetAllNetworks()
{
var query = (from n in _db.NetworkSet
.Include("NetworkContacts.Contact")
.Include("NetworkContacts.Contact.RelationshipSource.Target")
.Include("NetworkContacts.Contact.RelationshipSource.Source")
select (n));
return query;;
}
然后我尝试使用以下代码在我的 WebUI 层中填充我的 ViewModel
I then try to populate my ViewModel in my WebUI layer using the following code
var projectedNetworks =
from n in GetAllNetworks()
select new NetworkViewModel
{
Name = n.Name,
Contacts = from contact in networkList
.SelectMany(nc => nc.NetworkContacts)
.Where(nc => nc.Member == true)
.Where(nc => nc.NetworkId == n.ID)
.Select(c => c.Contact)
select contact,
};
return projectedNetworks;
问题现在出现在我新创建的 NetworkViewModel
中;Contacts
集合对象不包括 RelationshipSource.Target
或 RelationshipSource.Source
的任何加载数据.
The problem now occurs in my newly created NetworkViewModel
;
the Contacts
collection objects do not include any loaded data for RelationshipSource.Target
or RelationshipSource.Source
.
但是,当从原始存储库 IQueryable
方法运行时,数据就在那里.但是,当我使用 Select New NetworkViewModel {}
时,相关的 include
数据似乎没有传输到新的 Contacts
集合中.
However the data is there when run from the original Repository IQueryable
method. However the related include
data does not seem to get transferred into the new Contacts
collection that is created when I use the Select New NetworkViewModel {}
.
有没有办法在这个 Include
数据被传递到一个新对象时保留它?目前我只是不断收到 Null
异常
Is there a way to preserve this Include
data when it gets passed into a new object? At the moment I just keep getting Null
exceptions
推荐答案
但是,在您的情况下,我认为有一个更好 的解决方法,而不是链接文章中的解决方法.您已经在使用 Network
的视图模型.那挺好的!也为接触(以及它的相关属性)做这件事,你急切加载的问题会神奇地消失.投影总是有效.
In your case, however, I think there's a better workaround than what's in that linked article. You're already using a view model for Network
. That's good! Do it for contact (and it's related properties), too, and your problems with eager loading will magically go away. Projection always works.
这篇关于如果我从 IQueryable 中选择,则包含丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!