简体   繁体   English

EF过滤包括 - 许多子实体

[英]EF filtered include - many child entities

Having some issues with filtering child entities. 在过滤子实体时遇到一些问题。 I know that EF doesn't support filtered includes but I can't get any other alternative to work either. 我知道EF不支持过滤包含但我无法获得任何其他替代方案。

var q = from sWithA in
                    (from s in db.Svs
                     where s.Env.UID.Equals(env)
                     select new
                     {
                         Svs= s,
                         Cons= from c in s.Cons
                                      where c.Apps.Any(a => a.AppT.Type.Equals(appT))
                                      select c
                     }).AsEnumerable()
                select sWithA.Svs;
List<Svs> svsList = q.ToList();

This actually generates a SQL query which returns the information I need, but the child entities aren't being attached to the parent. 这实际上会生成一个SQL查询,它返回我需要的信息,但子实体没有附加到父实体。

svsList contains two Svs objects but the child collection (Cons) is empty. svsList包含两个Svs对象,但子集合(Cons)为空。

Svs < many-to-many > Cons < many to one > Apps Svs <many-to-many> Cons <many to one> Apps

Any ideas? 有任何想法吗?

The relationship fixup trick that you apparently try to exploit doesn't work for many-to-many relationships, only for one-to-one and one-to-many relationships. 您显然试图利用的关系修复技巧不适用于多对多关系,仅适用于一对一和一对多关系。 For many-to-many relationships you have to fixup the navigation collections manually, for example like so: 对于多对多关系,您必须手动修复导航集合,例如:

List<Svs> svsList = (from s in db.Svs
                     where s.Env.UID.Equals(env)
                     select new
                     {
                         Svs = s,
                         Cons = from c in s.Cons
                                where c.Apps.Any(a => a.AppT.Type.Equals(appT))
                                select c
                     })
                     .AsEnumerable()
                     .Select(sWithA =>
                     {
                         sWithA.Svs.Cons = sWithA.Cons.ToList();
                         return sWithA.Svs;
                     })
                     .ToList(); 

Yes, it's ugly and looking at this code one really misses the filtered Include support. 是的,它很丑陋,看着这个代码真的错过了过滤的Include支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM