Apparently I've gone silly on Wednesday morning. I have linq error that I should know the answer. Sorry for the stupid question.
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");
}
The error is on the foreach loop on the "results1" item.
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'
Can anyone push me in the right direction here?
CodePudding user response:
You are grabbing the first item in your query when you call .FirstOrDefault(). You can't iterate that with a foreach, it's a single item.
Based on your logic, your query is already grabbing the correct item ordered by 'Shipped_Date'. I think you want to just drop the foreach -
if (results1 != null)
{
lastDeliveryDate = results1.Shipped_Date;
}
else
{
lastDeliveryDate = Convert.ToDateTime("1/1/1980");
}