2013-11-27 37 views
0

這是我的字符串sql查詢,但我無法將它轉換爲linq,因爲它包含基於where子句的任何幫助,我們將不勝感激。linq中的where子句中的sql case語句

if (StrClientID.Equals("008")) 
        objdbhims.Query += " and ((case when t.external_org is null then d.clientid='008' end) or t.external_org in ('008','-1'))"; 
       else if (StrClientID.Equals("006")) 
        objdbhims.Query += " and (t.external_org<>'008' or (case when t.external_org is null then d.clientid='" + StrClientID + "' end))"; 
       else 
        objdbhims.Query += " and d.clientid='"+StrClientID+"'"; 
+0

可以使用三元運算符'在linq –

+0

@RaphaëlAlthaus案例語句替換是否如果其他但在三元運算符當我做和我不把其他部分,它給出了語法錯誤,我不想要其他部分,如何做到這一點/ –

+0

以及你可以在第二部分只是把類似'm => true' ... –

回答

-1

我解決它自己通過使用三元運算符與此解決方案很多掙扎後,我只是把1 == 1 else部分擺脫錯誤的:

string[] arr = new string[] {"008","-1"}; 

string ClientID = HttpContext.Current.Session["ClientID"].ToString(); 

&& (
           ClientID == "008" 
          ? (t.external_org == null 
               ? d.clientid =="008" : 1 == 1) 
               || (arr.Contains(t.external_org)) 


           : ClientID == "006" 
          ? (t.external_org == null 
               ? d.clientid == ClientID : 1 == 1) 
               || (t.external_org != "008") 

         : d.clientid == ClientID 
          ) 
+0

@Dmytro是它很好嗎? –

0

在從數據庫中獲取數據之前,您可能會在您的IQueryable對象中添加大量Where子句。例如:

var data = SomeContext.MyTable.Where(x => x.SomeField == someValue); 
if (someCondition) 
{ 
    // Something like your 1st if clause 
    data = data.Where(x => (x.SomeField2 != null && x.SomeField3 == "someText") || new[] { "001", "002" }.Contains(x.SomeField2)); 
} 

var result = data.ToList(); 
+0

如何做到這一點?....當t.external_org爲空然後d.clientid ='008'結束)或t.external_org在('008',' -1'))「 –

+0

它是根據表格列值進行檢查的,我在此基礎上添加了d.clientid =」008「 –

+0

它取決於您在Linq中t和d表之間的關係,例如,如果d是客戶端,並且t具有Client類型的屬性,那麼你可以寫t.Client.clientid ==「008」。如果t有一個客戶端列表並且你想獲得所有t記錄,其中t.Clients有一些客戶與特定的ID,你可以寫這個 - t.Clients.Any(y => y.clientid ==「008」) –