我需要使用Linq to Entity Framework来查询LOCATION表以获取具有MAX生效日期的位置代码的记录,然后在下一个查询中将该结果用作连接。 我相信我需要在使用IQueryable之前进行转换,因为我在第二个查询中有最后一个子句,我想要排除FLOOR代码在excludedSchools列表中的记录。 excludedSchools列表中将包含newLocationCode。 因此,我需要在使用之前更新IQueryable结果中的值。我可以这样做吗?这是我的代码:
using (var db = new TheContext())
{
IQueryable<LocationTable> locatinWithMaxEffDate =
(from lc in db.LocationTable
where lc.EFF_STATUS == "A" && lc.EFFDT <= DateTime.Now
group lc by lc.LOCATION into g
select g.OrderByDescending(x => x.EFFDT).FirstOrDefault()
);
foreach (var location in locatinWithMaxEffDate.ToList())
{
string newLocationCode;
if(codeMappingDictionary.TryGetValue(location.FLOOR, out newLocationCode))
{
// how do I update locatinWithMaxEffDate FLOOR value
// with newLocationCode so it works in the query below?
location.FLOOR = newLocationCode;
}
}
var query =
(from fim in db.PS_PPS_FIM_EE_DATA
join mloc in locatinWithMaxEffDate on fim.LOCATION equals mloc.LOCATION
where
fim.EMPL_STATUS == PsPpsFimEeData.EmployeeStatusValues.Active
&& fim.AUTO_UPDATE == PsPpsFimEeData.AutoUpdateValues.Enabled
&& includeJobCodes.Contains(fim.JOBCODE)
&& !excludedSchools.Contains(mloc.FLOOR)
select new PpsAdministratorResult
{
SchoolId = mloc.FLOOR,
Login = fim.OPRID,
EmployeeId = fim.EMPLID,
}
使用上面的代码,locatinWithMaxEffDate没有更新的FLOOR值。我可以看出为什么会这样,但似乎无法修复它。 到目前为止,我已经尝试将另一个列表引入ADD()新的位置记录,然后将其作为IQueryable进行转换,但是我得到了关于原始类型和具体类型的错误。
答案 0 :(得分:0)
我决定让自己更轻松。由于两组数据都非常小(每组少于1000条记录),我调用整个数据集作为匿名类型:
SELECT *
FROM Database
WHERE colLetter IN ('A', 'B', 'C')
ORDER BY colLetter
然后,只需使用这两个对象:
using (var db = new TheContext())
{
IQueryable<LocationTable> locatinWithMaxEffDate =
(from lc in db.LocationTable
where lc.EFF_STATUS == "A" && lc.EFFDT <= DateTime.Now
group lc by lc.LOCATION into g
select g.OrderByDescending(x => x.EFFDT).FirstOrDefault()
);
var query =
(from fim in db.PS_PPS_FIM_EE_DATA
join mloc in locatinWithMaxEffDate on fim.LOCATION equals mloc.LOCATION
where
fim.EMPL_STATUS == PsPpsFimEeData.EmployeeStatusValues.Active
&& fim.AUTO_UPDATE == PsPpsFimEeData.AutoUpdateValues.Enabled
&& includeJobCodes.Contains(fim.JOBCODE)
select new PpsAdministratorResult
{
SchoolId = mloc.FLOOR,
Login = fim.OPRID,
EmployeeId = fim.EMPLID,
}
}
现在,我有我想要的清单。