2011-03-19 152 views
1
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 or jL.JobId==0) 

它不能識別OR,我希望它總是返回JobId == 0。如何在Where子句中使用組合條件AND/OR編寫查詢?

我試過大寫,也試過parentheis其中((u.UserName == User.Identity.Name) OR (jL.JobId==0))

沒有OR,它工作得很好,這是一個語法編譯錯誤。

如何在Where子句中寫入組合條件AND/OR的var查詢?

回答

0

使用C#語言的正常AND(&/&&)和OR(|/||)操作符。

3

由於這是C#,你需要使用C#語法OR,這是|| (The Conditional OR operator)

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

此外 - 並且將被 「&&」:http://msdn.microsoft.com/en-us/library/2a723cdk.aspx – 2011-03-19 01:42:29

2

要使用C#運營商

  • & &並
  • | | for或

例如,

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) 
0

您需要使用||而不是或。

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

我不知道你將如何得到一個雖然的jobId,因爲它看起來像你想要做一個左連接。如果用戶不在jobLists表中,那麼對於jL.jobId它將爲空並且不返回任何內容。

更多左見this page加入

相關問題