开发者

How on write a query with combination condition AND/OR in Where Clause?

var query = from uM in db.aspnet_Memberships
                    join uD in db.UserDetails
                    on uM.UserId equals uD.UserId
                    join jL in db.JobLists
                    on uM.UserId equals jL.UserId
                    where (u.UserName == U开发者_如何学编程ser.Identity.Name or jL.JobId==0)

It doesn't recognize the OR, I want it always return JobId==0.

I tried uppercase, also tried parentheis where ((u.UserName == User.Identity.Name) OR (jL.JobId==0))

Without the OR, it works fine, it is a syntax compiler error.

How on write a var query with combination condition AND/OR in Where Clause?


Since this is C#, you need to use C# syntax for OR, which is || (The Conditional OR operator):

 (u.UserName == User.Identity.Name || jL.JobId == 0)


you want to use the c# operators

  • && for and
  • || for or

eg.

var query = from uM in db.aspnet_Memberships
            join uD in db.UserDetails
            on uM.UserId equals uD.UserId
            join jL in db.JobLists
            on uM.UserId equals jL.UserId
            where (u.UserName == User.Identity.Name || jL.JobId==0)


Use the normal AND (&/&&) and OR (|/||) operators of the C# language.


You need to use || instead of or.

((u.UserName == User.Identity.Name) || (jL.JobId==0))

I'm not sure how you will get a jobId though as it looks like you are wanting to do a left join. If the user does not exist in the jobLists table, then it will be null for jL.jobId and not return anything.

See this page for more on left joins

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜