将多个DbContext的LINQ结果合并到单个对象

时间:2016-11-30 10:44:01

标签: c# linq dbcontext

我有2个LINQ,每个都有不同的DbContext。我想将两者的结果添加到单个对象引用中。那就是peopleEntity和WebSyncDetailObject(引用linq select new {..})

类对象

public class WebSyncDetailEntity
{
    public Web_SyncMatchesEntity WebSyncMatch { get; set; }

    public Web_LookupsEntity WebLookups { get; set; }

    public List<PeopleEntity> People { get; set; }
}

LINQ处理类

 private List<WebSyncDetailEntity> ProcessGetWebSyncDetail()
    {
        List<WebSyncDetailEntity> WebSyncDetailObject = null;

        using (var _uow = new UCAS_WebSync_AdminTool_UOF())
        {
            try
            {
                List<PeopleEntity> peopleEntity = null ;

                int? personCode = _uow.Web_SyncMatchesRepository.GetAll().Where(x => x.SUBMISSION_ID == "28105").Select(y => y.PERSON_CODE).FirstOrDefault() ;

                using(var _fes_uow = new FES_UOF())
                {
                    peopleEntity = _fes_uow.PeopleRepository.GetAll().Where(x => x.PERSON_CODE == personCode).ToList();
                    var t = "d";
                }

               WebSyncDetailObject = (from esm in _uow.Web_SyncMatchesRepository.GetAll()
                                      join lup in (_uow.Web_LookupsRepository.GetAll().Where(a => a.RV_DOMAIN == "match_method")) on esm.MATCH_METHOD equals lup.LOOKUP_KEY
                                      where esm.SUBMISSION_ID == "28105"
                                      select new WebSyncDetailEntity {WebSyncMatch = esm, WebLookups = lup, People = peopleEntity }).ToList();

                //throw error here!
            }
            catch(Exception exp)
            {
                Debug.WriteLine(exp.ToString());
            }
        }

            return null;
    }

enter image description here

我想在WebSyncDetailEntity对象中找到最终结果

第二次错误

enter image description here

3 个答案:

答案 0 :(得分:1)

我认为您在此处获得例外的原因是它试图在发送到数据库的查询中包含peopleEntity。这不会起作用,因为查询中不支持复杂的数据类型。

一个简单的解决方法是在查询中首次创建People时将WebSyncDetailObject字段保留为空,然后在下一行将其设置为peopleEntity

答案 1 :(得分:0)

我认为你可以做这样的事情。

var resultsFromDb = (from esm in _uow.Web_SyncMatchesRepository.GetAll()
                                      join lup in (_uow.Web_LookupsRepository.GetAll().Where(a => a.RV_DOMAIN == "match_method")) on esm.MATCH_METHOD equals lup.LOOKUP_KEY
                                      where esm.SUBMISSION_ID == "28105" select new {esm = esm, lup = lup, peopleEntity = peopleEntity}).ToList(); // Here we break the linq to sql query

// Then run a select query on the above collection to get what we want.  
WebSyncDetailObject  = resultsFromDb.Select(r => new WebSyncDetailEntity {WebSyncMatch = r.esm, WebLookups = r.lup, People = r.peopleEntity }).ToList();

答案 2 :(得分:0)

我找到了答案;循环遍历列表并将对象添加到匹配条件

的现有列表对象
 public class WebSyncDetailEntity
{
    public Web_SyncMatchesEntity WebSyncMatch { get; set; }

    public Web_LookupsEntity WebLookups { get; set; }

    public PeopleEntity People { get; set; }
}

...

 private List<WebSyncDetailEntity> ProcessGetWebSyncDetail()
    {
        List<WebSyncDetailEntity> WebSyncDetailObject = null;

        using (var _uow = new UCAS_WebSync_AdminTool_UOF())
        {
            try
            {
                List<PeopleEntity> peopleEntity = null ;

                int? personCode = _uow.Web_SyncMatchesRepository.GetAll().Where(x => x.SUBMISSION_ID == "28105").Select(y => y.PERSON_CODE).FirstOrDefault() ;

                using(var _fes_uow = new FES_UOF())
                {
                    peopleEntity = _fes_uow.PeopleRepository.GetAll().Where(x => x.PERSON_CODE == personCode).ToList();
                }

                WebSyncDetailObject = (from esm in _uow.Web_SyncMatchesRepository.GetAll()
                                         join lup in (_uow.Web_LookupsRepository.GetAll().Where(a => a.RV_DOMAIN == "match_method")) on esm.MATCH_METHOD equals lup.LOOKUP_KEY
                                         where esm.SUBMISSION_ID == "28105"
                                         select new WebSyncDetailEntity { WebSyncMatch = esm, WebLookups = lup }).ToList();

                  int index = 0;
                  foreach(var item in WebSyncDetailObject)
                  {
                      WebSyncDetailObject[index].People = peopleEntity.Where(x => x.PERSON_CODE == WebSyncDetailObject[index].WebSyncMatch.PERSON_CODE).FirstOrDefault();
                      index++;
                  }
            }
            catch(Exception exp)
            {
                Debug.WriteLine(exp.ToString());
            }
        }
            return null;
    }