2015-04-01 63 views
-1

我有以下SQL表转换SQL查询与算术运算符,以LINQ

create table PurchasingShipments (
    ID INT IDENTITY(1,1) NOT NULL, 
    ShipmentID AS 'PSID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED PRIMARY KEY, 
    Title varchar(300), 
    NoOfPieces integer, 
    PricePerPiece money, 
    ActualCostPerPiece money, 
    Micelleneous money, 
    Year integer, 
    Month integer, 
    date integer, 
    TransportCost money, 
    SupplierCommission money, 
) 

而且我有下面的SQL查询来从上面的表中检索数据:

SELECT 
    YEAR(date) AS 'year', 
    SUM(NoOfPieces * PricePerPiece + Micelleneous + TransportCost + SupplierCommission) AS 'cost' 
FROM PurchasingShipments 
GROUP BY YEAR(date) 

我使用visual studio 2013与entityframework 6.所以我需要将上面的查询转换为LINQ。我怎样才能做到这一点?

+0

为什么'date'是整数数据类型? – 2015-04-01 05:44:25

回答

1

这可能是这样的:

var qry = from ps in PurchasingShipmentsEntity 
    group ps by ps.Year into grp 
    select new 
    { 
     Year = grp.Key, 
     Cost = grp.Sum(x=> x.NoOfPieces * x.PricePerPiece + x.Micelleneous + x.TransportCost + x.SupplierCommission) 
    };