How to convert this SQL query to LINQ (OVER (PARTITION BY Date))

Here is the query I'm trying to convert to Linq:

SELECT R.Code, 
       R.FlightNumber, 
       S.[Date], 
       S.Station,
       R.Liters, 
       SUM(R.Liters) OVER (PARTITION BY Year([Date]), Month([Date]), Day([Date])) AS Total_Liters
FROM S INNER JOIN
               R ON S.ID = R.SID
WHERE (R.Code = 'AC')
AND FlightNumber = '124'
GROUP BY  Station, Code, FlightNumber, [Date], Liter
ORDER BY R.FlightNumber, [Date]

Thanks for any help.

UPDATE: Here is the Linq code I'm trying on; I can not negotiate by date.

var test = 
(from record in ent.Records join ship in ent.Ship on record.ShipID equals ship.ID                       

orderby ship.Station
where ship.Date > model.StartView && ship.Date < model.EndView && ship.Station == model.Station && record.FlightNumber == model.FlightNumber

group record by new {ship.Station, record.Code, record.FlightNumber, ship.Date, record.AmountType1} into g

select new { g.Key.Station, g.Key.Code, g.Key.FlightNumber, g.Key.Date, AmmountType1Sum = g.Sum(record => record.AmountType1) });
+5
source share
2 answers

Run the query first without aggregation:

var test = 
(from record in ent.Records join ship in ent.Ship on record.ShipID equals ship.ID                       

orderby ship.Station
where ship.Date > model.StartView && ship.Date < model.EndView && ship.Station == model.Station && record.FlightNumber == model.FlightNumber

select new {ship.Station, record.Code, record.FlightNumber, ship.Date, record.AmountType1};

Then we calculate the amount

var result = 
    from row in test
    select new {row.Station, row.Code, row.FlightNumber, row.Date, row.AmountType1, 
    AmountType1Sum = test.Where(r => r.Date == row.Date).Sum(r => r.AmountType1) };

This should produce the same effect as a database query. The code above may contain errors, because I wrote it only here.

+3
source

I answered a similar thread: LINQ to SQL and the total by ordered results

In this thread, it was like this:

var withRuningTotals = from i in itemList    
                   select i.Date, i.Amount,    
                          Runningtotal = itemList.Where( x=> x.Date == i.Date).
                                                  GroupBy(x=> x.Date).
                                                  Select(DateGroup=> DateGroup.Sum(x=> x.Amount)).Single();

, , .

+1

All Articles