2016-09-15 40 views
5

我有這個疑問至極我想使用LINQ更換爲查詢語法:SQL ROW_NUMBER()在LINQ查詢語法

select 
    ii.Id, ii.Name as Supplier, 
    qi.Price1, qi.Price2, qi.Price3, 
    case when qi.price1 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price1,1000000) ASC) end AS Price1Order,  
    case when qi.price2 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price2,1000000) ASC) end AS Price2Order,  
    case when qi.price3 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price3,1000000) ASC) end AS Price3Order 
From dbo.InquiryItems ii 
left join dbo.quoteItems qi on ii.Id = qi.QuoteItem_InquiryItem 

SQL查詢結果:

Id  Supplier  Price1 Price2 Price3 Price1Order Price2Order Price3Order 
1655 Supplier 2  80.00 NULL 40.00 3   NULL  1 
1656 Supplier 4  65.00 30.00 42.00 2   1   2 
1662 Supplier 1  15.00 35.00 43.00 1   2   3 
1670 Supplier 3  250.00 NULL NULL 4   NULL  NULL 

在C#中,我需要這個查詢是一個IQueryable對象。我必須過濾不同部分(一個或多個)的查詢,然後按供應商(IdAccount)對其進行分組,然後合計價格。這個價格我必須排名。

return colQuoteItem = from vQuote in this.vQuoteItemOverviews 
    where vQuote.IdConstructionStageId == ConstructionStageId        
    group vQuote by new 
    { 
     vQuote.IdAccount 
    } into g                
    select new vQuoteItemOverviewSum 
    { 
     Id = g.Max(x => x.Id),         
     Price1Order = null, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     Price2Order = null, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     Price3Order = null, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     price1 = g.Sum(x => x.price1), 
     price2 = g.Sum(x => x.price2), 
     price3 = g.Sum(x => x.price3),         
    } 
    ; 

謝謝。

+1

的可能的複製[行\ _number超過在LINQ的(用xxx分區)?](http://stackoverflow.com/questions/9980568/row-number-over-partition-by-xxx-in- linq) – captainsac

+1

有點複雜,因爲你有3個領域想要row_number。您可以嘗試在這篇文章中的建議第一:http://stackoverflow.com/questions/365086/how-to-project-a-line-number-into-linq-query-results/365127#365127 – Prisoner

回答

1

這工作?

var qi1 = (from qi in quoteItems orderby qi.price1 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1}); 

var qi2 = (from qi in quoteItems orderby qi.price2 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1}); 

var qi3 = (from qi in quoteItems orderby qi.price3 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1}); 


return colQuoteItem = from vQuote in this.vQuoteItemOverviews.AsEnumerable() 
    where vQuote.IdConstructionStageId == ConstructionStageId        
    group vQuote by new 
    { 
     vQuote.IdAccount 
    } into g                
    select new vQuoteItemOverviewSum 
    { 
     Id = g.Max(x => x.Id),         
     Price1Order = qi1.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     Price2Order = qi2.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     Price3Order = qi3.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax 
     price1 = g.Sum(x => x.price1), 
     price2 = g.Sum(x => x.price2), 
     price3 = g.Sum(x => x.price3),         
    } 
    ; 
+0

感謝您的答案,它看起來非常好,但它也不起作用。 我得到這個錯誤: 你不能創建一個'匿名類型'類型的常量值。在這種情況下,只支持原始類型和枚舉類型。 謝謝。 –

+0

@HGRUser我不確定它是否能夠正常工作,但錯誤來自哪條線? – artm

+0

@HGRUser此外,它聽起來像是如果你爲'{i.QuoteItem_InquiryItem,Rank = index + 1}'創建一個類並用'IEnumerable qi1 ='和'new myClass(){Item = i'替換'var qi1 =' .QuoteItem_InquiryItem,Rank = index + 1}'可能會排除錯誤。 – artm