Linq Sub选择数据类型

时间:2017-06-28 16:25:29

标签: c# sql-server linq

学习Linq会是我的死,哈哈哈哈。

我有一个大型查询来检索报告的数据。我需要添加一个子查询来将相关数据导入结果集。子查询中的数据是数据库中的float。我的错误是"无法转换类型' System.Linq.IQueryable'到'漂浮'"

查询是:

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}))
};

问题是&#34;寄售&#34;选择中的行。我如何处理匿名类型并转换为浮点数?

谢谢!

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

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();

答案 1 :(得分:0)

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();

将其存储在数据表中。

感谢所有在正确方向上推动我的人。