-2

I am converting SQL to LINQ. Can someone help how to achieve the substring and case statements like below in LINQ.

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
Gilad Green
  • 36,708
  • 7
  • 61
  • 95

1 Answers1

0

This is how your sql will look like in 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)))
            };
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Index and length must refer to a location within the string. Parameter name: length This is the Exception which iam getting. If this is the Domian Name "Google\youtube " My expectation is Only "youtube" – Pankaj Errolla Aug 03 '16 at 06:24
  • Not yet Iam not able to get the domainame As expected which is returning -1 – Pankaj Errolla Aug 03 '16 at 09:16
  • Gilad Green Thank You – Pankaj Errolla Aug 03 '16 at 17:51
  • @PankajErrolla - what was the problem at the end? – Gilad Green Aug 05 '16 at 06:01
  • AliasName = p.DomainName == null ? null : p.DomainName.Substring(p.DomainName.IndexOf(@"@") + 1, p.DomainName.Length -p.DomainName.IndexOf(@"@")-1), this wayi resolved my problem and now facing issue with the Group by from two different tables how do we do that – Pankaj Errolla Aug 05 '16 at 06:57
  • @Ah ok I haven't realized that `p.DomainName` might be `null` – Gilad Green Aug 05 '16 at 06:59
  • from ci in orgContext.CreateQuery() join pc_mp in orgContext.CreateQuery() on ci.Id equals pc_mp.id into gr from leftJoin in gr.DefaultIfEmpty() join mp in orgContext.CreateQuery() on leftJoin.id equals mp.rId select new{} can you please help me on this iam getting an exception select to many is not supported followed by Join – Pankaj Errolla Aug 05 '16 at 08:55
  • @PankajErrolla - Please post it in a new question. If you'd like specifically me to help you with it then you can tag me in a comment – Gilad Green Aug 05 '16 at 09:19