2013-11-27 33 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”) –