0

I have simply code:

 Dim query as string = "SELECT id, red, yel, gre FROM table WHERE"
 if a then query=query + " red=true OR"
 if b then query=query + " yel=true OR"
 if c then query=query + " gre=true"
 if right(query,2)="OR" then query=left(query,len(query)-3)

How to make equivalent in LINQ ?

Update: (I need OR operator not AND like many other examples on the site)

1 Answers1

-1

If a,b and c are Boolean variables the below code works:

Dim result = dbContext.Table();
result = result.Where(Function(r) (a And r.red) Or (b And r.yel) Or (c And r.gre));
result = result.ToList();

But if you mean a,b and c are logic clauses and you are using .net method like .ToString() in them you have to change the code like below:

Dim result = dbContext.Table().ToList();
result = result.Where(Function(r) (a And r.red) Or (b And r.yel) Or (c And r.gre));

Because .net methods can't run on dbcontext, first call ToList() to transmit data and after that you can use all .Net method on transmited data.

Hasan Gholamali
  • 633
  • 4
  • 13