Linq-里面包括 [英] Linq - where inside include

查看:100
本文介绍了Linq-里面包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这将无法按书面要求进行,但我正努力寻找正确的答案,并且该非功能性代码有望说明我正在尝试实现的目标:

I know that this won't work as written, but I'm struggling to see the right answer, and this non-functional code hopefully illustrates what I'm trying to achieve:

var defaults = _cilQueryContext.DefaultCharges
                    .Where(dc => dc.ChargingSchedule_RowId == cs.RowId);

List<DevelopmentType> devTypes = 
        defaults.Select(dc => dc.DevelopmentType)
                .Include(d => d.DefaultCharges)
                .Include(d => d.OverrideCharges.Where(oc => oc.ChargingSchedule_RowId == cs.RowId))
                .Include(d => d.OverrideCharges.Select(o => o.Zone))
                .ToList();

从本质上讲,我认为这需要一个联接,但是由于要选择包含两种相关类型子项的父对象,因此看不到联接的"select new"子句中会发生什么. /p>

Essentially, I had presumed this required a join, but seeing as I'm trying to select a parent object containing two related types of children, I can't see what would go in the join's "select new" clause.

推荐答案

据我所知,Include不支持此类子查询.最好的选择是使用投影,例如

As far as I am aware Include does not support this type of sub-querying. Your best option is to use projection e.g.

List<DevelopmentType> devTypes = 
           defaults.Include(x => x.DefaultCharges)
                   .Include(x => x.OverrideCharges)
                   .Select(x => new {
                        DevType = x.DevelopmentType,
                        Zones = x.OverrideCharges.Where(oc => oc.ChargingSchedule_RowId == cs.RowId)
                                                 .Select(oc => oc.Zone).ToList()
                   })
                   .Select(x => x.DevType)
                   .ToList();

这篇关于Linq-里面包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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