繁体   English   中英

Linq select 语句在分组后不起作用

[英]Linq select statement not working after grouping

我通过加入从多个表中获取数据,并且我想根据日期对数据进行分组,但是在按语句分组之后,我收到 select 所有实体针对某个日期的错误。

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)

            };

而且我也在 select 语句中尝试了ToList() ,但它没有用。

尝试这个:

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

我们需要将查询分成两部分,因为 group by 子句没有完全转换为 SQL 所以在我的情况下,我想要一个月的数据,所以下面是我的代码片段。

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

       });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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