2016-04-27 44 views
0

能在下面的查询的儿童表的栏中适用条件吗?Linq-在儿童表的条件

病人是主表,我需要补充下面,其中有一列条件评估表像

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

完整的查询

=> 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() 
    ); 
+0

一对多的关系吗?如果其中一个孩子的价值是10而另一个孩子的价值是20呢?你想在查询中包含父项吗? –

+1

如果您只想让任何孩子的值为10,则可以使用以下条件:'a.Assess.Any(x => x.column1 == 10)'。 –

回答

1

可以使用其他子/包埋的λ lambda中的语句。在这种情况下,可以在Where语句中引用IEnumerable.Any语句。任何将返回true如果有任何条件匹配的lambda可以调用为Assess是一个集合。

我的假设是:

  • Assess是一个强类型集合
  • 你想要的任何Where子句,匹配,如果有收集其属性column等于价值的任何Assess情况10

代码:

=> 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() 
); 
+0

谢谢伊戈尔。赞赏。 – chint

+0

@chint - 很高兴帮助。此外,如果您的问题的答案是您认为最好的或最能帮助您的问题的答案,请使用该答案旁边的复选标记将答案标记为答案(支票将变为绿色)。您只能在每个问题中将1个答案标记为“正确”。 – Igor