从实体框架获取计数

时间:2012-12-06 08:36:34

标签: linq entity-framework

EF很新,基本上我想转换这个SQL查询:

SELECT     
PSKU.ProductSKUID, 
PSKU.ProductSKUName, 
W.WarehouseID, 
W.WarehouseName, 
SA.SystemAreaName, 
COUNT(SLI.ProductSKUID) AS QTY

FROM         dbo.StockLineItem AS SLI INNER JOIN
                      dbo.ProductSKU AS PSKU ON PSKU.ProductSKUID = SLI.ProductSKUID INNER JOIN
                      dbo.Warehouse AS W ON W.WarehouseID = SLI.WarehouseID INNER JOIN
                      dbo.SystemArea AS SA ON SA.SystemAreaID = SLI.SystemAreaID
WHERE     (SA.SystemAreaID = 1)
AND        W.WarehouseID = @WarehouseID
GROUP BY PSKU.ProductSKUID, PSKU.ProductSKUName, W.WarehouseName, SA.SystemAreaName, W.WarehouseID

要有效的EF声明。这就是我到目前为止,我的Model类和方法:

 [Serializable]
public class StockReturnMethod
{
    public int ProductSKUID { get; set; }
    public int WarehouseID { get; set; }
    public int LotID { get; set; }
    public string LotName { get; set; }
    public int AreaID { get; set; }
    public string AreaName { get; set; }
    public int BinID { get; set; }
    public string BinName { get; set; }
}


public class DALStockMovement
{

    scmEntitiesPrimaryCon entities = new scmEntitiesPrimaryCon();


    public List<AvailibleStock> AvailibleStockQty(int warehouseID)
    {
        var rows = (from PLA in entities.ProductLocationAssignments
                   from W in entities.Warehouses
                   from SLI in entities.StockLineItems
                   from SA in entities.SystemAreas
                   from PSKU in entities.ProductSKUs


                  where W.WarehouseID == warehouseID
                  select new AvailibleStock() { WarehouseID = W.WarehouseID, ProductSKUID = PSKU.ProductSKUID, ProductSKUName = PSKU.ProductSKUName, WarehouseName = W.WarehouseName, Status = SA.SystemAreaName, QtyUnassigned = SLI.ProductSKUID  }).ToList();
        return rows;
    }

任何有关获得有效EF声明的建议都将不胜感激

1 个答案:

答案 0 :(得分:0)

我实际上使用了这个名为Linqer的工具,因为我有SQL 我只是将它弹出到该工具中,它为我生成了Linq。

以下是出现的内容:

   var SKUStock = (from sli in entities.StockLineItems
                            where
                              sli.SystemArea.SystemAreaID == 1 &&
                              sli.WarehouseID == warehouseID
                            group new { sli.ProductSKU, sli.Warehouse, sli.SystemArea, sli } by new
                            {
                                ProductSKUID = (System.Int32?)sli.ProductSKU.ProductSKUID,
                                sli.ProductSKU.ProductSKUName,
                                sli.Warehouse.WarehouseName,
                                sli.SystemArea.SystemAreaName,
                                WarehouseID = (System.Int32?)sli.Warehouse.WarehouseID
                            } into g
                            select new AvailibleStock()
                            {
                                ProductSKUID = (int)(System.Int32?)g.Key.ProductSKUID,
                                ProductSKUName = g.Key.ProductSKUName,
                                WarehouseID = (int)(System.Int32?)g.Key.WarehouseID,
                                WarehouseName = g.Key.WarehouseName,
                                Status = g.Key.SystemAreaName,
                                QtyUnassigned = (int)(Int64?)g.Count(p => p.sli.ProductSKUID != null)
                            }).ToList();
            return SKUStock;

它完全返回我需要的东西:)。