簡體   English   中英

Linq子選擇數據類型

[英]Linq Sub Select Data Type

學習Linq將是我的死,哈哈哈哈。

我有一個大型查詢來檢索報告數據。 我需要添加一個子查詢,以將相關數據放入結果集中。 子查詢中的數據是數據庫中的浮點數。 我的錯誤是“無法將類型'System.Linq.IQueryable'轉換為'float'”

查詢是:

var results = from d in db.Deliveries
join j in db.Jobs on d.Job equals j.Job1
join c in db.Customers on j.Customer equals c.Customer1
join ml in db.Material_Locations on j.Part_Number equals ml.Material into t1
from t2 in t1.DefaultIfEmpty()
join uv in db.User_Values on j.User_Values equals uv.User_Values into t3
from t4 in t3.DefaultIfEmpty()
where d.Promised_Date >= calFrom.SelectedDate && d.Promised_Date <= calTo.SelectedDate
where d.Remaining_Quantity > 0
where (t2.Location_ID ?? "") != "MSSICONSMT"
orderby d.Job, c.Name, d.Promised_Date
select new
{
      d.Promised_Date,
      d.Job,
      LocationID = t2.Location_ID ?? "",
      d.Shipped_Quantity,
      d.Remaining_Quantity,
      d.Promised_Quantity,
      j.Unit_Price,
      on_Hand_Qty = ((double?)t2.On_Hand_Qty) ?? 0.0,
      Part_Number = j.Part_Number ?? "",
      c.Name,
      c.Ship_Lead_Days,
      SafetyStk = t4.Decimal1 ?? 0.0,
      ShipDate = d.Promised_Date.AddDays(-1 * (c.Ship_Lead_Days ?? 0)),
      RemainValue = d.Remaining_Quantity * j.Unit_Price,
      Balance = d.Remaining_Quantity > 0 ? d.Remaining_Quantity - (((double?)t2.On_Hand_Qty) ?? 0.0) : 0,
      Consignment = ((float)(from x in db.Material_Locations
                        join jx in db.Jobs on x.Material equals jx.Part_Number
                        where jx.Job1 == d.Job
                                && x.Location_ID == "MSSICONSMT"
                        select new {x.On_Hand_Qty}))
};

問題在於選擇中的“寄售”行。 如何處理匿名類型並將其轉換為浮點數?

謝謝!

嘗試以下操作:

Consignment = (from x in db.Material_Locations
               join jx in db.Jobs on x.Material equals jx.Part_Number
               where jx.Job1 == d.Job && x.Location_ID == "MSSICONSMT"
               select new {qty = (float)x.On_Hand_Qty}).ToList();
Consignment = (from x in db.Material_Locations
                                     where x.Material == j.Part_Number
                                         && x.Location_ID == "MSSICONSMT"
                                     select (float?)x.On_Hand_Qty ?? 0.0).ToList()

我必須添加這個

newRow["Consignment"] = thisRow.Consignment.FirstOrDefault();

將其存儲在數據表中。

感謝所有向正確方向推動我的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM