我有一个LINQ查询,它输出ToList,除了每次运行时都会更新原始记录而不是创建新记录外,它的工作情况还不错。 每次运行此代码时,data.EventID都会更改,因此我希望每条记录都出现在列表中。
代码:
foreach(var data in vehicleqry)
{
bool inUK = boundryChecker.IsLongLatInUK((double)data.declatfloat, (double)data.declongfloat);
if (inUK == true)
{
var qryevent = (from e in db.events
where e.eventID == data.EventID
select new
{
e.eventID,
e.sysdatetime,
e.vehicleID
}).ToList();
}
{
答案 0 :(得分:1)
我认为您真正想要的是只运行一个查询而不是循环查询。您可以通过使用Contains
方法来做到这一点:
var vehicleqry = ...;
// Get all of the individual event IDs for entries that are "inUK"
var vehicleEventIds = vehicleqry
.Where(ve => boundryChecker
.IsLongLatInUK((double)ve.declatfloat, (double)ve.declongfloat)
.Select(ve => ve.EventID);
// Get all the matching events
var qryevent = (from e in db.events
where vehicleEventIds.Contains(e.eventID)
select new
{
e.eventID,
e.sysdatetime,
e.vehicleID
}).ToList();