顯然我在星期三早上發瘋了。我有 linq 錯誤,我應該知道答案。對不起這個愚蠢的問題。
var results1 = (from p in db1.a_PCRs
join je in db1.a_Job_Extensions on p.PCR equals je.PCR
join j in db1.Jobs on je.Job equals j.Job1
join d in db1.Deliveries on j.Job1 equals d.Job
where j.Status == "Active"
where j.Job1.StartsWith("A")
where p.PCR == "2552"
where d.Shipped_Quantity > 0
orderby d.Shipped_Date descending
select new
{
p.PCR,
d.Shipped_Date
}).FirstOrDefault();
if (results1 != null)
{
foreach (var result1 in results1)
{
lastDeliveryDate = result1.Shipped_Date;
}
}else
{
lastDeliveryDate = Convert.ToDateTime("1/1/1980");
}
錯誤出現在“results1”專案的 foreach 回圈中。
Error CS1579 foreach statement cannot operate on variables of type '<anonymous type: string PCR, DateTime? Shipped_Date>' because '<anonymous type: string PCR, DateTime? Shipped_Date>' does not contain a public instance or extension definition for 'GetEnumerator'
誰能在這里把我推向正確的方向?
uj5u.com熱心網友回復:
當您呼叫 .FirstOrDefault() 時,您正在獲取查詢中的第一項。你不能用 foreach 來迭代它,它是一個單一的專案。
根據您的邏輯,您的查詢已經獲取了由“Shipped_Date”訂購的正確商品。我想你只想放棄 foreach -
if (results1 != null)
{
lastDeliveryDate = results1.Shipped_Date;
}
else
{
lastDeliveryDate = Convert.ToDateTime("1/1/1980");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510653.html