0
List<System.Linq.Expressions.Expression<Func<MyType, bool>>> lstPredicates = new List<System.Linq.Expressions.Expression<Func<MyType, bool>>>();

foreach (MyType myAccount in lstMyType)
{
    System.Linq.Expressions.Expression<Func<MyType, bool>> predicate = 
        t => t.Account == myAccount.Account && t.Branch == myAccount.Branch;
    lstPredicates.Add(predicate);
}

lstTransactions = Context.MyTrans
   .Where(lstPredicates)
   .ToList();

I am trying to run the lookup in MyTrans table only once, so I am building up a list of predicates. I want to retrieve transactions in a list where any of the account and branch combinations exists in transactions.

i.e. I am trying to produce a predicate like

predicate = t => 
    (t.Account == 123 && t.Branch == London) 
    || (t.Account == 433 && t.Branch == Manchester)
    ||...
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
wk4questions
  • 125
  • 1
  • 2
  • 11

1 Answers1

0

You could use Linqkit library. Using PredicateBuilder you can do the following:

 var predicate = PredicateBuilder.False<MyType>();

 foreach (MyType myAccount in lstMyType)
 {
   predicate = predicate.Or (
        t => t.Account == myAccount.Account && t.Branch == myAccount.Branch);
 }

var query= Context.MyTrans.AsExpandable().Where(predicate);
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
ocuenca
  • 38,548
  • 11
  • 89
  • 102