使用多个case语句转换ORDER BY

时间:2018-11-23 05:50:16

标签: c# linq

我需要隐蔽在SQL查询下面作为LINQ查询。我该如何使用LINQ在case by子句中放置条件if语句?

 SELECT   
     td1.TYPEDEFDESC +':'+ td2.TypeDefDesc as ResponseTypeReason,  
     convert(varchar(20),td1.TypeDefid) +'~'+ convert(varchar(20),td2.TypeDefcode) as ResponseTypeCode,  
     td1.TYPEDEFID,  
     td1.TYPEDEFGROUP,  
     td1.TYPEDEFCODE,  
     td1.TYPEDEFDESC,  
     td1.PARENTID  
     FROM TYPEDEFINITION td1 WITH (NOLOCK)  
     join TypeDefinition td2 with (nolock)   
     on td1.TypeDefid = td2.ParentId  
     WHERE td1.TypeDefGroup='ResponseType'  
     and td1.Active=1   
     and td2.Active=1  
     order by   
     case when td1.TypeDefDesc='Successful' and td1.TypeDefGroup='ResponseType' then 1  
     when td1.TypeDefDesc='Failed' and td1.TypeDefGroup='ResponseType' then 2  
     when td1.TypeDefDesc='Failed Attempt' and td1.TypeDefGroup='ResponseType' then 3 end asc  

下面没有案例说明的我的转换

objTypeDefLst = (from t1 in objTypeDefLst join t2 in objTypeDefLst
                 on t1.TypeDefid equals TUtil.CheckInt(t2.ParentId,0)
                 where t1.TypeDefGroup == strTypeDefGrp  
                 orderby(t1.TypeDefGroup)                                     
                 select new TypeDefinition {  
                       ResponseTypeReason = (t1.TypeDefDesc +":" +t2.TypeDefDesc),
                       ResponseTypeCode = t1.TypeDefid +"~" + t2.TypeDefcode
                                        }).ToList();

3 个答案:

答案 0 :(得分:0)

尝试在orderby语句本身内部使用三元ifelse运算符,像这样根据您的需要进行修改

objTypeDefLst = (from t1 in objTypeDefLst
                    join t2 in objTypeDefLst on t1.TypeDefid equals TUtil.CheckInt(t2.ParentId, 0)
                    where t1.TypeDefGroup == strTypeDefGrp
                    orderby (t1.TypeDefDesc == "Successful" && td1.TypeDefGroup == "ResponseType" ? 1 : (td1.TypeDefDesc == "Failed" && td1.TypeDefGroup == "ResponseType" ? 2 : (td1.TypeDefDesc == "Failed Attempt" && td1.TypeDefGroup == "ResponseType" ? 3 : 4)))
                    select new TypeDefinition
                    {
                        ResponseTypeReason = (t1.TypeDefDesc + ":" + t2.TypeDefDesc),
                        ResponseTypeCode = t1.TypeDefid + "~" + t2.TypeDefcode
                    }).ToList();

答案 1 :(得分:0)

您应该可以使用Ternary Operator来做到这一点。

三元运算符使您可以将有些long的if/else语句转换为单行。例如:

int a;
if(test) {
    a = 1;
} else {
    a = 2;
}

可以转换为:

int a = test ? 1 : 2;

也可以通过使用多个三元表达式来制作更复杂的表达式。

例如

int a;
if(testA) {
    a = 1;
} else if(testB) {
    a = 2;
} else {
    a = 3;
}

可以转换为此:

int a = testA ? 1 : // if(testA)
        testB ? 2 : // else if(testB)
        3; // else

另一种格式为:

int a = (testA ? 1 : (testB ? 2 : 3));

在您的情况下,可以在orderby子句中使用三元运算符:

objTypeDefLst = (from t1 in objTypeDefLst join t2 in objTypeDefLst
            on t1.TypeDefid equals TUtil.CheckInt(t2.ParentId,0)
            where t1.TypeDefGroup == strTypeDefGrp  
            orderby 
                ((t1.TypeDefDesc == "Successful" && 
                 t1.TypeDefGroup == "ResponseType") ? 1 :
                (t1.TypeDefDesc == "Failed" && 
                 t1.TypeDefGroup == "ResponseType") ? 2 :
                (t1.TypeDefDesc == "Failed Attempt" && 
                 t1.TypeDefGroup == "ResponseType") ? 3 :
                 4) // Whatever the "else" group is
            select new TypeDefinition {  
                   ResponseTypeReason = (t1.TypeDefDesc +":" +t2.TypeDefDesc),
                   ResponseTypeCode = t1.TypeDefid +"~" + t2.TypeDefcode
            }).ToList();

查看MSDN docs以获得更多信息。

答案 2 :(得分:0)

由我的工作完成。使用 ThenBy

objTypeDefLst = (from t1 in objTypeDefLst
                                     join t2 in objTypeDefLst
                                         on t1.TypeDefid equals TUtil.CheckInt(t2.ParentId, 0)
                                     where t1.TypeDefGroup.ToUpper().Trim() == strTypeDefGrp.ToUpper().Trim()
                                     select new TypeDefinition
                                     {
                                         ResponseTypeReason = (t1.TypeDefDesc + ":" + t2.TypeDefDesc),
                                         ResponseTypeCode = t1.TypeDefid + "~" + t2.TypeDefcode
                                     }).ToList();
                    objTypeDefLst = objTypeDefLst.OrderBy(a => a.TypeDefDesc == strTypeDefDescSuccess && a.TypeDefGroup == strTypeDefGrp)
                                    .ThenBy(a => a.TypeDefDesc == strTypeDefDescFailed && a.TypeDefGroup == strTypeDefGrp)
                                    .ThenBy(a => a.TypeDefDesc == strTypeDefDescFailedAtte && a.TypeDefGroup == strTypeDefGrp).ToList();