toxic toxic - 3 days ago 4
C# Question

Store LINQ result into view-model refereeing to multiple class objects instead of storing LINQ result in var

I have linq statement that gives three class object data

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();


enter image description here

I want to store this LINQ query result into viewModel as instead of '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; }
}


Need to fix here!



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

Answer

Change your view model

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

    public Web_EBS_SyncEntity EBS_SyncEntity { get; set; }

    public Web_SyncAuditLogEntity SyncAuditLogEntity { get; set; }
}

and then

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();

I haven't tested the code, just suggesting you a way to go, looking for better solution.

Comments