在下次查询中用作连接之前更新IQueryable结果

问题描述 投票:0回答:1

我需要使用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进行转换,但是我得到了关于原始类型和具体类型的错误。

c# linq iqueryable
1个回答
0
投票

我决定让自己更轻松。由于两组数据都非常小(每个少于1000条记录),我调用整个数据集作为匿名类型:

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,
         }  
}

然后,只需使用两个对象:

List<PpsAdministratorResult> administratorList = new List<PpsAdministratorResult>();
foreach (var location in query.ToList())
{
    string newLocationCode;
    if(schoolCodeMappings.TryGetValue(location.SchoolId, out newLocationCode)) // && newLocationCode.Contains(location.LOCATION))
    {
        location.SchoolId = newLocationCode;
    }
    if( !excludedSchools.Contains(location.SchoolId) )
    {
        administratorList.Add(location);
    }
}

现在,我有我想要的清单。

© www.soinside.com 2019 - 2024. All rights reserved.