0

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ステートメントへ。これは私がこれまでに持っているもの、私のモデルクラスとメソッドです:

 [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ステートメントにするためのアドバイスをいただければ幸いです。

4

1 に答える 1

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;

それは私が必要なものを正確に返します:)。

于 2012-12-06T12:20:37.710 に答える