简体   繁体   中英

Linq select statement not working after grouping

I am getting data from multiple tables by joining and I want to group data on basis of date but after group by the statement, I'm getting an error to select all entities against a date.

var query = from record in _entityRepository.GetAll().Where(x => x.DateRecord > DateTime.UtcNow.Date)
            join job in _jobRepository.GetAll() on record.Id equals job.Id
                into g1
            from job in g1.DefaultIfEmpty()
            join punchList in _punchListRepository.GetAll() on record.Id equals punchList.Id
                into g2 from punchList in g2.DefaultIfEmpty()
            join punchJob in _jobRepository.GetAll() on punchList != null ? -1 : punchList.JobId equals punchJob.Id
                into g4 from punchJob in g4.DefaultIfEmpty()
            group new {record, job, punchList, punchJob} by new{ record.DateRecord}
            into g3
            select new
            {
                Date = g3.Key,
                job= g3.Select(x=>x.job),
                punchList= g3.Select(x=>x.punchList)

            };

And I also have tried ToList() in select statement but it did not work.

Try this:

var entityRepository=_entityRepository.GetAll().Where(x => x.DateRecord > DateTime.UtcNow.Date).ToList();
var jobRepository=_jobRepository.GetAll().ToList();;
var punchListRepository=_punchListRepository.GetAll().ToList();;


var query = from record in entityRepository
            
            join job in jobRepository on record.Id equals job.Id into g1
            from job in g1.DefaultIfEmpty()
            
            join punchList in punchListRepository on record.Id equals punchList.Id into g2 
            from punchList in g2.DefaultIfEmpty()
            
            join punchJob in jobRepository on punchList != null ? -1 : punchList.JobId equals punchJob.Id into g4 
            from punchJob in g4.DefaultIfEmpty()
            
            group new {record, job, punchList, punchJob} by new{ record.DateRecord} into g3
            select new
            {
                Date = g3.Key,
                job= g3.Select(x=>x.job).ToList(),
                punchList= g3.Select(x=>x.punchList).ToList()
            };

We need to split the query into two parts because group by clause is not fully converted into SQL so in my case I want one-month data so below is my code snipet.

var query = from record in _entityRepository.GetAll().Where(x =>
                x.DateRecord > DateTime.UtcNow.Date && x.DateRecord <= DateTime.UtcNow.Date.AddMonths(1))
            join job in _jobRepository.GetAll() on record.Id equals job.Id
                into g1
            from job in g1.DefaultIfEmpty()
            join punchList in _punchListRepository.GetAll() on record.Id equals punchList.Id
                into g2
            from punchList in g2.DefaultIfEmpty()
            join punchJob in _jobRepository.GetAll() on punchList != null ? -1 : punchList.JobId equals punchJob.Id
                into g4
            from punchJob in g4.DefaultIfEmpty()
            select new {record, job, punchList,punchJob};

var queryResult = await query.ToListAsync();
var result = queryResult.GroupBy(x => x.record.DateRecord)
       .Select(o => new
       {
           Date = o.Key,
           job = o.Select(x => x.job ?? new Job()).ToList(),
           punchList = o.Select(x => x.punchList ?? new PunchList()).ToList()

       });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM