I have the following (note how I am using LINQ within a foreach):
foreach (string recid in recids)
{
var recprec = (from rc in db.tblTrucks
where rc.ID == recid
select rc
}).FirstOrDefault();
}
if (recprec.TruckMake == "GM")...
I need to do some further processing based on recprec after the foreach but I get the following:
recprec does not exist in the current context.
Not sure how to fix this. I tried to do something like Object recprec = null; but still no luck.
Well, the error is because you’re declaring
recprecinside theforeachloop. It loses scope once the loop exits.You could move the additional processing inside the loop:
Just understand that it will be done once for every iteration. There isn’t enough information in your question to determine if that’s what you really want.