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
C# Linq OrderBy取决于条件_C#_Linq_Lambda_Sql Order By - Fatal编程技术网

C# Linq OrderBy取决于条件

C# Linq OrderBy取决于条件,c#,linq,lambda,sql-order-by,C#,Linq,Lambda,Sql Order By,我想订一份名单,具体取决于考试结果 if(group.Substring(group.Length-1,1)%2==0) 降序 else 升序 List<CellTemp> orderedCells = (from shelf in foundCells where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1), 1) % 2 == 0 orderby

我想订一份名单,具体取决于考试结果

if(group.Substring(group.Length-1,1)%2==0)
降序
else
升序

List<CellTemp> orderedCells = 
        (from shelf in foundCells
         where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1), 1) % 2 == 0
         orderby shelf.Grup, shelf.Row descending
         select new CellTemp()
         {
             cod= shelf.cod,
             PN = shelf.PN,
             Description = shelf.Description,
             Group= shelf.Group,
             Row= shelf.Row,
             Shelf= shelf.Shelf
         }).ToList();

但是列表中有0个元素

也许这就是您想要的:

 var orderCells = (from shelf in celuleGasite
     where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1, 1)) % 2 == 0
     orderby shelf.Group, shelf.Row descending)
     .Concat(from shelf in celuleGasite
             where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1, 1)) % 2 == 1
             orderby shelf.Group, shelf.Row)
     .ToList();
或者使用
GroupBy

var orderCells = celuleGasite.GroupBy(shelf=>Convert.ToInt32(shelf.Group[shelf.Group.Length-1]) % 2)
                             .Select(g=>g.Key == 0 ? g.OrderBy(shelf=>shelf.Group)
                                                      .ThenByDescending(shelf=>shelf.Row) :
                                                     g.OrderBy(shelf=>shelf.Group)
                                                      .ThenBy(shelf=>shelf.Row))
                             .SelectMany(x=>x)
                             .ToList();

在国王的帮助下,我设法找到了解决办法。他的代码可能是从头开始写的:)所以如果你像我一样陷入困境,下面是完整的代码

 var orderCells1 = (from shelf in foundCells
            where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1, 1)) % 2 == 0
            orderby shelf.Row descending
            select shelf).Concat(from shelf in foundCells
            where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1, 1)) % 2 == 1
            orderby shelf.Row
            select shelf).OrderBy(grup => grup.Group).ToList();
或者使用GroupBy

var orderCells2 = foundCells.GroupBy(shelf=>Convert.ToInt32(shelf.Group[shelf.Group.Length - 1]) % 2)
  .Select(g => g.Key == 0 ? 
               g.OrderByDescending(shelf => shelf.Row) : g.OrderBy(shelf => shelf.Row))
  .SelectMany(x => x).OrderBy(group=>group.Group).ToList();

该声明太长,便于阅读和维护。如果将所有子查询存储在一个名称明确的变量中,那么回答这个问题也会更容易。谢谢Alex,我编辑了这篇文章。@geogesc你的
工具架的类型是什么。Row
?shelf.Row是这种格式的字符串“Row_1”谢谢你的想法。我现在开始让它工作。正如您在这里看到的,它没有正确排序。它应该按“Grup”顺序排列,如果Grup的最后一个数字是奇数,那么具有相同Grup的筏应该按降序排列。
var orderCells2 = foundCells.GroupBy(shelf=>Convert.ToInt32(shelf.Group[shelf.Group.Length - 1]) % 2)
  .Select(g => g.Key == 0 ? 
               g.OrderByDescending(shelf => shelf.Row) : g.OrderBy(shelf => shelf.Row))
  .SelectMany(x => x).OrderBy(group=>group.Group).ToList();