9

这是我试图转换为 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]

谢谢你的帮助。

更新:这是我正在尝试的 Linq 代码;我无法按日期进行过度分区。

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

2 回答 2

5

先执行查询而不进行聚合:

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

然后计算总和

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

这应该产生与数据库查询相同的效果。上面的代码可能有错误,因为我只写在这里。

于 2012-06-30T23:57:05.127 回答
1

我已经回答了一个类似的主题:LINQ to SQL and a running total on ordered results

在那个线程上是这样的:

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();

在您的情况下,您可能必须在分组时首先将两个表连接在一起,然后在连接的表结果上运行上述相同的概念。

于 2014-10-01T20:51:52.793 回答