PredicateBuilder Where is the list inside the list with C #

I have a problem with the PredicateBuilder class.

I have class 3.

public class A
{
  public int val1 {get;set;}
  public int val2 {get;set;}
  public List<B> listb {get;set;}
}

public class B
{
  public int val3 {get;set;}
  public int val4 {get;set;}
  public List<C> listc {get;set;}
}

how can I find val3 in class B I need a search like:

var query = PredicateBuilder.True<A>();
query = query.And(x => x.listb.Where(b=> b.val3 == 1);
+4
source share
1 answer

Just replace .Where()with .Any()to create a true / false boolean condition:

query.And(x => x.listb.Any(b => b.val3 == 1));

This will return all records Awhere any element in listbcontains val3of 1. If you only want to Awrite down where all the elements in listbmatch the condition, use .All():

query.And(x => x.listb.All(b => b.val3 == 1));
+3
source

Source: https://habr.com/ru/post/1648567/


All Articles