【问题标题】:Store LINQ result into view-model refereeing to multiple class objects instead of storing LINQ result in var将 LINQ 结果存储到视图模型中,以引用多个类对象,而不是将 LINQ 结果存储在 var 中
【发布时间】:2024-04-29 20:55:02
【问题描述】:

我有给出三个类对象数据的 linq 语句

1- appForm 2- ebsSync 3- SyncAuditLog

var _AppForms2 = (from appForm in _uof.Web_AppFormsRepository.GetAll()
                          join syncAuditLog in (_uof.Web_SyncAuditLogRepository.GetAll().
                                                 Where(sal => sal.LOG_STATUS.Equals("EP") &&
                                                       sal.LOOKUP_ID != null &&
                                                       sal.ID == maxAuditID)
                                                 .Select(shortListedAuditLog => new { shortListedAuditLog })) on appForm.SUBMISSION_ID equals syncAuditLog.shortListedAuditLog.SUBMISSION_ID
                          join ebsSync in _uof.Web_EBS_SyncRepository.GetAll() on appForm.SUBMISSION_ID equals ebsSync.SUBMISSION_ID
                          select new {appForm , ebsSync, syncAuditLog }).ToList();

我想将此 LINQ 查询结果存储到 viewModel 中,而不是 'var _AppForms';

 public class WebSyncSummaryEntity
{
    public List<Web_AppFormsEntity> AppFormsEntity { get; set; }

    public List<Web_EBS_SyncEntity> EBS_SyncEntity { get; set; }

    public List<Web_SyncAuditLogEntity> SyncAuditLogEntity { get; set; }
}

这里需要修复!

 public List<WebSyncSummaryEntity> GetWebSyncSummary()
    {
        List<WebSyncSummaryEntity> _AppForms = null;

        using (var _uof = new UCAS_WebSync_AdminTool_UOF())
        {

            var maxAuditID = (from sal in _uof.Web_SyncAuditLogRepository.GetAll().Where(
                                                                                   sal => sal.LOG_STATUS.Equals("EP") &&
                                                                                   sal.LOOKUP_ID != null)
                              select sal).Max(x => x.ID);


   _AppForms = (from appForm in _uof.Web_AppFormsRepository.GetAll()
                          join syncAuditLog in (_uof.Web_SyncAuditLogRepository.GetAll().
                                                 Where(sal => sal.LOG_STATUS.Equals("EP") &&
                                                       sal.LOOKUP_ID != null &&
                                                       sal.ID == maxAuditID)
                                                 .Select(shortListedAuditLog => new { shortListedAuditLog })) on appForm.SUBMISSION_ID equals syncAuditLog.shortListedAuditLog.SUBMISSION_ID
                          join ebsSync in _uof.Web_EBS_SyncRepository.GetAll() on appForm.SUBMISSION_ID equals ebsSync.SUBMISSION_ID
                          select new {appForm , ebsSync, syncAuditLog }).ToList();

            var test = "d";
        }

        return _AppForms;
    }

【问题讨论】:

  • 您的查询返回 1 个包含 3 个对象的列表。而不是返回包含 3 个列表的 1 个对象。因此,您应该对数据进行分组。

标签: c# entity-framework linq viewmodel


【解决方案1】:

改变你的视图模型

public class WebSyncSummaryEntity
{
    public Web_AppFormsEntity AppFormsEntity { get; set; }

    public Web_EBS_SyncEntity EBS_SyncEntity { get; set; }

    public Web_SyncAuditLogEntity SyncAuditLogEntity { get; set; }
}

然后

var _AppForms2 = (from appForm in _uof.Web_AppFormsRepository.GetAll()
                          join syncAuditLog in (_uof.Web_SyncAuditLogRepository.GetAll().
                                                 Where(sal => sal.LOG_STATUS.Equals("EP") &&
                                                       sal.LOOKUP_ID != null &&
                                                       sal.ID == maxAuditID)
                                                 .Select(shortListedAuditLog => new { shortListedAuditLog })) on appForm.SUBMISSION_ID equals syncAuditLog.shortListedAuditLog.SUBMISSION_ID
                          join ebsSync in _uof.Web_EBS_SyncRepository.GetAll() on appForm.SUBMISSION_ID equals ebsSync.SUBMISSION_ID
                          select new WebSyncSummaryEntity {AppFormsEntity =appForm , EBS_SyncEntity =ebsSync, SyncAuditLogEntity =syncAuditLog }).ToList();

我没有测试过代码,只是给你建议一个方法,寻找更好的解决方案。

【讨论】:

  • 很高兴知道,愉快的编码