LINQ to SQL中的子字符串和case语句

时间:2016-08-03 06:01:03

标签: c# mysql sql-server linq linq-to-sql

我正在将SQL转换为LINQ。有人可以帮助如何在LINQ中实现如下所示的子字符串和case语句。

SELECT p.goalcommitment, 
       g.goalmeasurement,
       CASE g.goalmeasurement 
           WHEN 'Count' THEN CAST(p.goalcommitmentnumber AS VARCHAR)
           WHEN 'Currency' THEN '$' + CAST(p.goalcommitmentcurrency AS VARCHAR)
           WHEN 'Percentage' THEN RTRIM(CAST(p.goalcommitmentpercentage AS VARCHAR)) + '%'
           ELSE p.pamwb_goalcommitment 
       END AS goalcommitment,
       SUBSTRING(p.domainname, CHARINDEX('\', p.domainname, 0) + 1, LEN(p.domainname) - CHARINDEX('\', p.domainname, 0))    
FROM Filtered_psp p
JOIN Filtered_goalsubtype g 
ON g.goalsubtypeid = p.goalsubtype

1 个答案:

答案 0 :(得分:0)

这就是你的sql在linq中的样子:

var query = from p in Filtered_psp
            join g in Filtered_goalsubtype on p.goalsubtype equals g.goalsubtypeid
            let slashLocation = p.domainname.IndexOf(@"\")
            select new
            {
                Field1 = p.goalcommitment,
                Field2 = g.goalmeasurement,
                Field3 = (p.goalmeasurement == "Count" ? p.goalcommitmentnumber.ToString() :
                          p.goalmeasurement == "Currency" ? "$" + p.goalcommitmentnumber.ToString() :
                          p.goalmeasurement == "Percentage" ? p.goalcommitmentnumber.ToString() + "%" : p.pamwb_goalcommitment),
                Field4 = slashLocation == -1 ? (p.domainname : p.domainname.Substring(slashLocation + 1, p.domainname.Length - slashLocation - 1)))
            };