Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL到LINQ的转换_C#_Sql_Linq_Linq To Sql - Fatal编程技术网

C# SQL到LINQ的转换

C# SQL到LINQ的转换,c#,sql,linq,linq-to-sql,C#,Sql,Linq,Linq To Sql,我需要帮助生成实现以下SQL查询所需的C#代码。我不确定这是否可能 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-4D

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

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;
试试这个:

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

到目前为止你试过什么?至少开始实施LINQ解决方案,并尽可能做到最好;至少要处理一些简单的事情。@Servy很好,很公平,我已经用我以前使用的代码编辑了我的文章。我很难适应分区和行号()