4

I have a problem with PredicateBuilder Class.

I have a 3 class like.

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 search val3 in B class I need a search like :

var query = PredicateBuilder.True<A>();
query = query.And(x => x.listb.Where(b=> b.val3 == 1);
Caner
  • 813
  • 1
  • 12
  • 26

1 Answers1

3

Simply 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 A records where any item in listb contains a val3 of 1. If you only want A records where all items in listb match the condition, use .All():

query.And(x => x.listb.All(b => b.val3 == 1));
David L
  • 32,885
  • 8
  • 62
  • 93