2016-09-15 2 views
5

Ich habe diese Abfrage weicht ich mit Linq als Abfragesyntax ersetzen will:SQL ROW_NUMBER() in LINQ Abfragesyntax

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-Abfrage-Ergebnis:

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 

In C# Ich brauche Diese Abfrage als ein IQueryable-Objekt. Ich muss die Abfrage für verschiedene Teile (eine oder mehrere) filtern und dann nach Lieferant (IdAccount) und SUM die Preise gruppieren. Diese Preise muss ich einstufen.

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),         
    } 
    ; 

Danke.

+1

Mögliche Duplikat [Row \ _number über (Partition von xxx) in Linq?] (Http://stackoverflow.com/questions/9980568/row-number-over-partition-by-xxx-in- linq) – captainsac

+1

ein bisschen kompliziert, weil du 3 felder haben willst row_number. Sie können Vorschlag in diesem Beitrag zuerst versuchen: http://stackoverflow.com/questions/365086/how-to-project-a-line-number-into-linq-query-results/365127#365127 – Prisoner

Antwort

1

Würde das funktionieren?

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

Danke für Ihre Antwort, es sieht sehr gut aus, aber es funktioniert auch nicht. Ich habe diesen Fehler: Sie können keinen konstanten Wert vom Typ 'Anonymer Typ' erstellen. In diesem Kontext werden nur primitive Typen und Aufzählungstypen unterstützt. Danke. –

+0

@HGRUser Ich war mir nicht sicher, ob es trotzdem funktionieren würde, aber aus welcher Zeile stammt der Fehler? – artm

+0

@ HGRUser Es klingt auch, wenn Sie eine Klasse für '{i.QuoteItem_InquiryItem, Rank = index + 1}' 'erstellen und' var qi1 = 'durch' IEnumerable qi1 = 'und' new myClass() {Item = i ersetzen .QuoteItem_InquiryItem, Rank = index + 1} ', wodurch der Fehler möglicherweise behoben wird. – artm