简体   繁体   中英

Linq Sub Select Data Type

Learning Linq is going to be the death of me, hahahaha.

I have a large query to retrieve data for a report. I need to add a sub query to get related data into the result set. The data in the subquery is a float in the database. My error is "Cannot convert type 'System.Linq.IQueryable' to 'float'"

The query is:

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

The problem is the "Consignment" line in the select. How do I deal with the anonymous type and get to converted into a float?

Thanks!

Try following :

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

and I had to add this;

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

to store it in the datatable.

Thanks to all who nudge me in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM