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/9/visual-studio/7.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# Linq:与GroupBy和SelectMany一起添加额外条件_C#_Linq - Fatal编程技术网

C# Linq:与GroupBy和SelectMany一起添加额外条件

C# Linq:与GroupBy和SelectMany一起添加额外条件,c#,linq,C#,Linq,继续我以前的职务: 我有以下SQL表: Name Description Id UserId CreatedDate UserSet1 Desc1 0 Abc 06/01/2018 UserSet1 Desc2 1 Abc 06/01/2018 UserSet1 Desc4 2 Def 06/02/2018 UserSet2 Desc for 2 5

继续我以前的职务:

我有以下SQL表:

Name       Description   Id   UserId   CreatedDate

UserSet1   Desc1         0    Abc      06/01/2018
UserSet1   Desc2         1    Abc      06/01/2018
UserSet1   Desc4         2    Def      06/02/2018
UserSet2   Desc for 2    5    NewUser  06/04/2018
UserSet2   Desc for 2    7    NewUser  06/19/2018
我想从上表中提取的只是每个名称的最新Id,以便获得以下输出

Name      Description    Id    UserId    CreatedDate

UserSet1  Desc1          0     Def       06/01/2018
UserSet1  Desc2          2     Def       06/01/2018
UserSet2  Desc for 2     7     NewUser   06/19/2018
由于ID2和ID7是表中UserSet1和UserSet2的最新条目,因此我想显示它,而不是表中的所有条目。现在用下面的LINQ代码解决了这个问题。现在我有另一个要求,也要显示ID为零的所有行,以及上面的预期结果

var results = response
  .GroupBy(row => row.Name)
  .SelectMany(g => g
     .OrderByDescending(row => row.Id)
     .Take(1));
我如何修改我上面的Linq,以便它显示该名称的所有最高的no ID以及任何同样为零的ID


谢谢

谢谢,就这样。我错过了工会声明。
  var results = response.GroupBy(row => row.Name)
                        .SelectMany(g => g
                        .OrderByDescending(row => row.Id)
                        .Take(1))
                      .Union(response.Where(x=>x.Id== 0));