2016-09-15 9 views
5

私は、クエリ構文としてLINQのと交換したい、このクエリウィッヒがありますSQL ROW_NUMBER()

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-クエリ結果:私は必要なのC#で

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 

をこのクエリをIQueryableオブジェクトとして使用します。私は異なる部分(1つまたは複数)のクエリをフィルタリングし、サプライヤ(IdAccount)でグループ化し、価格をSUMする必要があります。この価格は私がランクする必要があります。

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}'のクラスを作成し、 'var qi1 ='を 'IEnumerable qi1 ='と 'new myClass(){Item = i .QuoteItem_InquiryItem、Rank = index + 1} 'を実行してエラーを取り除くことができます。 – artm