繁体   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