-2

我需要帮助生成所需的 C# 代码来实现以下 SQL 查询。我不确定这是否可能?

select p.Name, r.Id, r.Status, r.Created, r.DepartmentId
from (select r.*, row_number() over (partition by personid order by created desc) as seqnum
      from checkin r
      where r.departmentid = '7AF20674-AEC1-4D4C-B1EA-88B1D7E8F3DB' and cast(created as date) = '2013-02-11'
     ) r join
     person p
     on r.personid = p.id
where seqnum = 1
order by p.name desc

以前我每人每天只有一个注册,所以以下是可能的。现在我可以每人每天进行几次注册,这就是 SQL 更新的原因。

    var query = from b in context.Persons
                join c in context.CheckIns
                    on b.Id equals c.PersonId into JoinedPersCheck
                from c in JoinedPersCheck.Where(i => i.Date.Equals(date)).DefaultIfEmpty()
                where b.DepartmentId.Equals(departmentId)
                orderby b.Name

                select new Model.PersonCheckIn
                {
                    CheckIn = MapToCheckInModel(c),
                    Person = MapToModel(b),
                };
    return query;
4

1 回答 1

0

尝试这个:

var query = context.CheckIns
               .Where(p => p.DepartmentId == departmentId && p.Created.Date == date)
               .GroupBy(r => r.PersonId)
               .Select(g => g.OrderByDescending(p => p.Created).First())
               .Join(context.Persons, c => c.PersonId, p => p.Id, 
                        (c,p) => new { p.Name, c.Id, c.Status, c.Created, c.DepartmentId})
               .OrderByDescending(f => f.Name);
于 2013-02-11T21:14:55.093 回答