【问题标题】:LINQ SelectMany returns child instead of a parent in nested filteringLINQ SelectMany 在嵌套过滤中返回子代而不是父代
【发布时间】:2022-01-11 19:15:45
【问题描述】:
  var someQuery = context.Parents.Select(c => c.SomeChildren.SelectMany(rm => rm.SomeGrandchildren.Where(r => r.Type == "Type 1")));

此代码/查询返回 SomeChildren 对象。我想做同样的过滤,但返回父对象。出于某种原因,这不是直截了当的。我尝试应用“Where”而不是“Select”,但语法没有给我选项。

如何使用相同的嵌套过滤返回父对象

非常感谢您

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    你愿意试试这个吗?

    var sompeQuery = context.Parents
      .Where(c => c.SomeChildren.SelectMany(x => x.SomeGrandChildren)
         .Any(r => r.Type == "Type 1")
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多