0

我想根据结果订购一个列表

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

我怎样才能保持第一个shelf.Group OrderBy 和OrderBy Shelf.row 升序或降序取决于shelf.Group 是奇数还是偶数?

Shelf.group 的格式是“Group_A0”。

--------------------编辑---------------

对困惑感到抱歉。我想做这样的事情。

var orderCells = (from shelf in celuleGasite
  where Convert.ToInt32(shelf.Gruup.Substring(shelf.Group.Length - 1, 1)) % 2 == 0
  orderby shelf.Group, shelf.Row descending
  where Convert.ToInt32(shelf.Group.Substring(shelf.Group.Length - 1, 1)) % 2 == 1
  orderby shelf.Group, shelf.Row ascending 
  select shelf).ToList();

但列表有 0 个元素

4

2 回答 2

2

也许这就是你想要的:

 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();
于 2013-08-10T23:58:28.603 回答
0

在 King King 的帮助下,我设法找到了解决方案。他的代码可能是从头开始编写的 :) 所以如果你像我一样被卡住,这里是完整的代码。

 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();
于 2013-08-11T09:12:13.733 回答