Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

Or Condition in Entity Framework

I am working on entity framework project, i have to apply Or condition in dbContext.Where I have tried this but its giving me error "Operator || cannot be applied to operand of types lambda expressions"

return dataContext.Friends
              .Where((r => r.ToUserId == touserid && r.FromUserId == fromuserid) 
                  || (r => r.ToUserId == fromuserid&& r.FromUserId == touserid ))
              .ToList();

I also tried using && instead of || but its giving me same error for &&,how can i apply Or condition for this senario? I have tried without brackets as well

Upvotes: 0

Views: 3887

Answers (3)

Manoj Dwivedi
Manoj Dwivedi

Reputation: 95

@Syed

|| (r => r.ToUserId == fromuserid&& r.FromUserId == touserid )).ToList();

Should be

|| (r => r.ToUserId == fromuserid && r.FromUserId == touserid )).ToList();

note the space between the "fromuserid&&".

Upvotes: -2

Matthias Meid
Matthias Meid

Reputation: 12513

Put it into one lambda that includes the || instead of ||ing two separate lambas:

return dataContext.Friends.Where(r => (r.ToUserId == touserid && r.FromUserId == fromuserid) || (r.ToUserId == fromuserid&& r.FromUserId == touserid)).ToList();

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

You need to do it like this:

return dataContext.Friends.Where(r => (r.ToUserId == touserid && r.FromUserId == fromuserid) || (r.ToUserId == fromuserid && r.FromUserId == touserid))
                          .ToList();

The only difference is that I deleted the second r => and fixed the parenthesis.

Upvotes: 5

Related Questions