ar.gorgin
ar.gorgin

Reputation: 5002

Ho to use If condition in LINQ Where clause

Can I use if clause with Linq where?

for example: I have a table with 4 filed(Id,UserId,Type,Sing).

I want select recorde that userid is 1 and if Type="None" , Sing should True.

(from d in db.Receive
 where ((((d.SendType == "None") ? ((d.Signed) ? true : false) : true)) && userid==1)
 select d).ToList(); 

when i use

((d.SendType == "None") ? ((d.Signed) ? true : false) : true)

it is select records that if Type="None" , Sing should True. but when add userid condition , don't return any records.

Upvotes: 0

Views: 5515

Answers (3)

Tony
Tony

Reputation: 7445

Isn't it better:

from d in db.Receive
where ((d.SendType == "None" && d.Signed) || d.SendType != "None") && userid == 1)
select d 

Upvotes: 2

Andrew
Andrew

Reputation: 2335

Theres lots of brackets there...if I understand you correctly userid == 1 is used regardless so why not just have

where (((d.SendType == "None" && userid == 1) ? ((d.Signed) ? true : false) : true)))
              select d).ToList(); 

Upvotes: 0

Mikhail Timofeev
Mikhail Timofeev

Reputation: 2169

and so?

(((d.SendType == "None") && (userid == 1)) ? ((d.Signed) ? true : false) : true)

Upvotes: 0

Related Questions