我正在使用实体框架工作,并且我正在尝试加入。连接执行得很好,但是当我尝试循环结果时,我收到错误。见下文
var items = from a in db.ActivityLogs
from v in db.Vehicles
where (a.PlateID1 == v.PlateID || a.PlateID2 == v.PlateID) && v.Alerted == true
select new { a, v };
执行正常,然后我收到错误
附加信息:无法投射类型'<> f__AnonymousType0`2 [DB.ActivityLog,DB.Vehicle]'输入' DB.ActivityLog'。
以下代码
foreach (ActivityLog l in items)
提前致谢
答案 0 :(得分:3)
您已投射到匿名类型,因此无法将其投放到活动日志。
var items = (from a in db.ActivityLogs
from v in db.Vehicles
where (a.PlateID1 == v.PlateID || a.PlateID2 ==
v.PlateID) && v.Alerted == true
select new { a, v }).ToArray();
foreach (var item in items)
{
Console.writeLine(item.a);
Console.writeLine(item.v);
}
或
var items = (from a in db.ActivityLogs
from v in db.Vehicles
where (a.PlateID1 == v.PlateID || a.PlateID2 ==
v.PlateID) && v.Alerted == true
select new ActivityLog
{
A= a, // Pseudo code
V = v, // Pseudo code map to essential properties
}).ToArray();
foreach (ActivityLog l in items)