繁体   English   中英

Linq在组中找到持久记录

[英]Linq to find lasted record in group

我想添加一个新列以查找组中的持久记录。
我可以在Select()方法中编写子查询吗?
我尝试了这个

var test = DailyPeriods.Where(x => x.BookingDate == "2016/12/30")
    .Select(x =>
        new
        {
            PERIOD_GROUP_ID = x.PeriodGroupID,
            PERIOD_NAME = x.PeriodName,
            New_Column = DailyPeriods
                            .Where(z => z.BookingDate == "2016/12/30")
                            .Select(a =>
                                new
                                {
                                    PeriodGroupID = a.PeriodGroupID,
                                    period_name = a.PeriodName
                                }
                            )
                            .GroupBy(b => b.period_name)
                            .Select(g => g.Last().PeriodGroupID)
                            .Contains(x.PeriodName)
        })

但是会发生这个错误
“列不在范围内:A:2211708.C(BOOKING_DATE)”

在此处输入图片说明

尝试这个..

var lastRecords = periodList.GroupBy(l => l.PeriodName)
                            .Select(x => new { PeriodName = x.Key,
                                               PeriodGroupId = x.OrderBy(l => l.PeriodGroupId).Last().PeriodGroupId});

var result = from period in periodList
             from lastRec in lastRecords.Where(r => r.PeriodGroupId == period.PeriodGroupId 
                                                    && r.PeriodName == period.PeriodName)
                                        .DefaultIfEmpty()
             select new { period.PeriodGroupId,period.PeriodName, New_Column=lastRec==null?false:true };

暂无
暂无

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

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