简体   繁体   English

Linq-子表上的条件

[英]Linq - where condition on child table

can we apply where condition on a column in child table in below query ? 我们可以在下面的查询的子表中的列上应用where条件吗?

Patient is main table and i need to supplement below where condition with a column in Assess table like 患者是主表,我需要在以下条件下用“评估”表中的列来补充

Where(a => a.Date >= startdate && a.Date < stopdate && a.patient.assess.column1 == 10)

full query 完整查询

  => dc.Patient
        .Include("first")
        .Select(a => new
        {
            Patient = a,
        Date = a.Assess.Max(x => x.Date),
        M3 = a.M3,
        Assess = a.Assess,
        Details = a.Assess
                    .Select(x => new
            {
                x.Key,
            x.Team
            })
        })
        .Where(a => a.Date >= startdate && a.Date < stopdate)
        .OrderBy(a => a.Date)
        .Take(batchSize)
        .ToList()
    );

You can use additional sub/embedded lambda statements within a lambda. 您可以在lambda中使用其他子/嵌入的lambda语句。 In this case the IEnumerable.Any statement can be referenced inside the Where statement. 在这种情况下,可以在Where语句中引用IEnumerable.Any语句。 Any will return true if there is any condition that matches the lambda and can be called as Assess is a collection. 如果存在与lambda匹配的任何条件,并且可以将其称为Assess是一个集合,则Any会返回true

My assumptions were: 我的假设是:

  • Assess is a strongly typed collection Assess是一个强类型集合
  • You wanted any the Where clause to match if there were any Assess instances in the collection where the property column was equal to the value of 10 如果集合中有任何Assess实例的property column等于10的值,则您希望任何Where子句都匹配

Code: 码:

=> dc.Patient
    .Include("first")
    .Select(a => new
    {
        Patient = a,
        Date = a.Assess.Max(x => x.Date),
        M3 = a.M3,
        Assess = a.Assess,
        Details = a.Assess.Select(x => new
        {
            x.Key,
            x.Team
        })
    })
    .Where(a => a.Date >= startdate && a.Date < stopdate && a.Assess.Any(ass => ass.column1 == 10))
    .OrderBy(a => a.Date)
    .Take(batchSize)
    .ToList()
);

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

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