Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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/6/codeigniter/3.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
Linq 林克,怎么做群比?_Linq_Linq To Sql - Fatal编程技术网

Linq 林克,怎么做群比?

Linq 林克,怎么做群比?,linq,linq-to-sql,Linq,Linq To Sql,项目表: Id、部门、年份、名称、级别 Id = 1, DeptId = 1, Year = 2000, Name = "ABC", Level = 1 Id = 2, DeptId = 1, Year = 2001, Name = "ABC1", Level = 1 Id = 3, DeptId = 1, Year = 2002, Name = "ABC2", Level = 1 Id = 4, DeptId = 2, Year = 2000, Name = "ABC3", Level =

项目表:

Id、部门、年份、名称、级别

Id = 1, DeptId = 1, Year = 2000, Name = "ABC", Level = 1
Id = 2, DeptId = 1, Year = 2001, Name = "ABC1", Level = 1
Id = 3, DeptId = 1, Year = 2002, Name = "ABC2", Level = 1

Id = 4, DeptId = 2, Year = 2000, Name = "ABC3", Level = 1
Id = 5, DeptId = 2, Year = 2002, Name = "ABC4", Level = 1

Id = 6, DeptId = 3, Year = 2000, Name = "ABC5", Level = 1
Id = 7, DeptId = 3, Year = 2001, Name = "ABC6", Level = 1
Id = 8, DeptId = 3, Year = 2002, Name = "ABC7", Level = 1
我有一个项目表。我需要得到2001年每个部门的项目。对于DeptId=2,它没有2001年的项目,我需要显示上一年的项目,即2000年的项目

我的linq应该返回以下结果

Id = 2, DeptId = 1, Year = 2001, Name = "ABC1", Level = 1
Id = 4, DeptId = 2, Year = 2000, Name = "ABC3", Level = 1
Id = 7, DeptId = 3, Year = 2001, Name = "ABC6", Level = 1
我正在考虑使用groupby,将DeptId分组,但不确定如何编写此查询

更新:

这就是我试过的

var projects=project.Where(x=>x.Year==2001)

但这不会返回DeptId=2的结果,这就是为什么我考虑使用groupby,但不知道如何编写它

var query=来自context.Projects中的p
var query= from p in context.Projects
           group p by p.DeptId into grp
           select grp.Where(x => x.Year <= 2001)
                     .OrderByDescending(x => x.Year)
                     .FirstOrDefault();
p.DeptId将p分组为grp 选择grp.Where(x=>x.Year x.Year) .FirstOrDefault();
来自上下文中的l.Projects.Where(w=>w.年w.年g.DeptId)
.Select(s=>s.LastOrDefault());
或者:

    context.Projects.Where(w => w.Year <= 2001)
        .GroupBy(g => g.DeptId)
        .Select (s => s.OrderBy(o => o.Year).LastOrDefault());
context.Projects.Where(w=>w.Year g.DeptId)
.Select(s=>s.OrderBy(o=>o.Year).LastOrDefault());

如果项目未按年份排序,该怎么办?
    context.Projects.Where(w => w.Year <= 2001)
        .GroupBy(g => g.DeptId)
        .Select (s => s.LastOrDefault());
    context.Projects.Where(w => w.Year <= 2001)
        .GroupBy(g => g.DeptId)
        .Select (s => s.OrderBy(o => o.Year).LastOrDefault());