【问题标题】:Linq Sub Select Data TypeLinq Sub 选择数据类型
【发布时间】:2017-06-28 16:25:28
【问题描述】:

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

问题是选择中的“寄售”行。如何处理匿名类型并转换为浮点数?

谢谢!

【问题讨论】:

  • 尝试 select new {(float)x.On_Hand_Qty} 而不是强制转换 IQueryable。
  • 另外,与您的问题无关,但您确实应该将这种查询逻辑推送到您的 DBMS,可能通过存储过程或视图。
  • 无法更改数据库,是外部数据库。

标签: c# sql-server linq


【解决方案1】:

尝试以下:

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

【讨论】:

  • 给定的代码可以编译,但是现在当我运行时,我必须将寄售转换为双精度才能放入数据表中。当我尝试将此值分配给数据表中的双字段时,我收到“无法将类型为'System.Collections.Generic.List1[&lt;&gt;f__AnonymousType181[System.Single]]'的对象转换为类型'System. IConvertible'。无法在寄售列中存储 1[<>f__AnonymousType181[System.Single]]>。预期类型为 Double。"我尝试使用 Convert.ToDouble 但没有成功。
  • 更改自:选择新的 {qty = (double)x.On_Hand_Qty}).ToList(); To : select (float)x.On_Hand_Qty).ToList();
  • 无法在寄售列中投射“System.Collections.Generic.List1[System.Single]' to type 'System.IConvertible'.Couldn't store &lt;System.Collections.Generic.List1[System.Single]> 类型的对象。预期类型为 Double。
  • 我通过 LinqPad 运行查询,发现很多时候该值是空值。
  • 将(浮动)更改为(浮动?)
【解决方案2】:
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();

将其存储在数据表中。

感谢所有将我推向正确方向的人。

【讨论】:

    猜你喜欢
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2011-08-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多