我有以下sql查询,我能够参加LINQ
select count(*), BatchID
from TimeSheetHeader
group by BatchID having count(*) > 1
转换为
var duplicateBatchIDList = from c in _db.TimeSheetHeaders
group c by c.BatchID into grp
where grp.Count() > 1
select grp;
但是现在我正在尝试抛出一个不包含在该组中的额外列。我有SQL查询
select count(*), BatchID, TimeSheetCreationDate = min( TimeSheetCreationDate )
from TimeSheetHeader
where TimeSheetCreationDate >= CURRENT_TIMESTAMP
group by BatchID having count(*) > 1
但我不确定这如何转换为LINQ
答案 0 :(得分:0)
from c in _db.TimeSheetHeaders
where c.TimeSheetCreationDate >= CURRENT_TIMESTAMP
group c by c.BatchID into grp
where grp.Count() > 1
select new
{
BatchID = grp.Key,
Count = grp.Count(),
TimeSheetCreationDate = grp.Min(x => x.TimeSheetCreationDate)
}
答案 1 :(得分:0)
您可以在此处找到查询的lambda表达式版本:
var result = TimeSheetHeader
.Where(t => t.TimeSheetCreationDate >= TIMESTAMP)
.GroupBy(t => t.BatchID)
.Where(g => g.Count() > 1)
.Select(g => new
{
Count = g.Count(),
BatchID = g.Key,
TimeSheetCreationDate = g.Min(x => x.TimeSheetCreationDate)
})
.ToList();
答案 2 :(得分:-1)
我稍微修改了马格努斯的回答
var duplicateBatchIDList = (from c in _db.TimeSheetHeaders
group c by c.BatchID into grp
where grp.Count() > 1
select new
{
BatchID = grp.FirstOrDefault().BatchID,
TimeSheetCreationDate = grp.Min(x => x.TimeSheetCreationDate)
}).Where(x => x.TimeSheetCreationDate <= DateTime.Now);