I have the following sql below which I am having real trouble getting into a LINQ statement. The specific issue is that for one of the fields I am using a CASE statement both in the select and has part of the HAVING clause. Various reasons why i needed to do this but I cant seem to reproduce it in LINQ.
Here is the sql Queryselect Count (DetailID) as MonthCount ,StoreID, DetailID,SUM(Amount ) as AnnualAmount, PAYear,MIN(CASE when ExportFlag = 'SUCCESS' then 1 when ExportFlag is NULL orExportFlag = '' then 0 else - 1 end) as ExpFlagfrom Input_Financialsinner join DestinationDetail cdd oncdd.ID =Financials.DetailIDgroup by StoreID, DetailID, PAYearHaving MIN (CASE when ExportFlag = 'SUCCESS' then 1 when ExportFlag is NULLthen 0 else -1 end ) = 0and Count (*) >= 1order by PAYearConvert This query in Linq to Sqlvar query = from inf in inputActsjoin cgrdets in cgrDetails on inf.DestinationDetailIDequals cgrdets.IDlet export = (inf.ExportFlag == "SUCCESS" ? 2:inf.ExportFlag== "ERROR" ? 1:inf.ExportFlag == null ? 0: 99)group inf by new {export, inf.StoreID,inf.DestinationDetailID,inf.PAYr } into gwhere g.Key.export == 0select new { MonthCount = g.Count(), g.Key.StoreID,g.Key.PAYr,Amount = g.Sum(inf=>inf.Amount) };please ignore some minor discrepancies you may find in naming of fields between these two - its just probably because i renamed some quickly for the sake of this question.
No comments:
Post a Comment