简体   繁体   English

linq中where子句中的sql case语句

[英]sql case statement in where clause in linq

Here is my string sql query, but i am unable to convert it to linq as it contains cases based where clause, any help will be appreciated. 这是我的字符串sql查询,但是由于包含基于case的where子句,因此我无法将其转换为linq,我们将不胜感激。

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+"'";

You may add a lot of Where clauses to your IQueryable object before fetching data from DB. 在从数据库中获取数据之前,可以在IQueryable对象中添加很多Where子句。 For example: 例如:

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();

I solved it myself after struggling alot with this solution by using ternary operator, i just put 1==1 in the else part to get rid of error: 在使用三元运算符为此解决方案苦苦挣扎之后,我自己解决了这个问题,我只是在else部分中放入1 == 1来消除错误:

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
                            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM