Home > Net >  LINQ SelectMany returns child instead of a parent in nested filtering
LINQ SelectMany returns child instead of a parent in nested filtering

Time:12-07

  var someQuery = context.Parents.Select(c => c.SomeChildren.SelectMany(rm => rm.SomeGrandchildren.Where(r => r.Type == "Type 1")));

This code/query returns the SomeChildren objects. I would like to do the same filtering but return the Parent objects. For some reason this is not straight forward. I tried to apply 'Where' instead of 'Select' but syntax isn't giving me options.

How can I return Parent object using the same nested filtering

Thank you very much in advance

CodePudding user response:

would you kindly try this ?

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