2015-04-01 104 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) 
    };