LINQ.按关键字排序的列表,但第一组带有条件。

huangapple go评论60阅读模式
英文:

LINQ. ordered list by key group but first group are with condition

问题

我有一个按键组排序的列表。

var productList = CustomerData.Data.Products
                .GroupBy(x => x.CategoryDesc)
                .OrderBy(x => x.Key)
                .ToDictionary(x => x.Key, x => x.ToList());

它工作得很好。

我想要一个特定的组(带有销售)排在列表的第一位。
但其他组应按降序排列。
类似这样:

LINQ.按关键字排序的列表,但第一组带有条件。

***我以这种格式写是为了方便查看

[
  {
    "Key": "SPECIFIC",
    "Value": [
      {
        "SubGroup": "wire cutters",
        "Model": "A",
        "Price": "65"
      },
      {
        "SubGroup": "hammers",
        "Model": "B",
        "Price": "71"
      },
      {
        "SubGroup": "hammers",
        "Model": "C",
        "Price": "92.5"
      },
      
    ]
  },
  {
    "Key": "Rank A",
    "Value": [
      {
        "SubGroup": "hammers",
        "Model": "A",
        "Price": "130"
      },
      {
        "SubGroup": "hammers",
        "Model": "B",
        "Price": "142"
      },
      {
        "SubGroup": "hammers",
        "Model": "C",
        "Price": "185"
      },
      
    ]
  },
  {
    "Key": "Rank B",
    "Value": [
      {
        "SubGroup": "pliers",
        "Model": "A",
        "Price": "95"
      },
      {
        "SubGroup": "pliers",
        "Model": "B",
        "Price": "59"
      },
      {
        "SubGroup": "pliers",
        "Model": "C",
        "Price": "65"
      },
      
    ]
  },
  {
    "Key": "Rank C",
    "Value": [
      {
        "SubGroup": "saws",
        "Model": "A",
        "Price": "905"
      },
      {
        "SubGroup": "saws",
        "Model": "B",
        "Price": "589"
      },
      {
        "SubGroup": "saws",
        "Model": "C",
        "Price": "655"
      },
      
    ]
  },
  
]
英文:

I"m have List ordered by key group.

var productList = CustomerData.Data.Products
                .GroupBy(x => x.CategoryDesc)
                .OrderBy(x => x.Key)
                .ToDictionary(x => x.Key, x => x.ToList());

it's good working

And i want one SPECIFIC group (with sale) set as first in the list.
But the rest of the groups to stay sorted in descending order.
something like that:
LINQ.按关键字排序的列表,但第一组带有条件。

***I wrote in the format for the convenience of seeing

[
  {
    "Key": "SPECIFIC",
    "Value": [
      {
        "SubGroup": "wire cutters",
        "Model": "A",
        "Price": "65"
      },
      {
        "SubGroup": "hammers",
        "Model": "B",
        "Price": "71"
      },
      {
        "SubGroup": "hammers",
        "Model": "C",
        "Price": "92.5"
      },
      
    ]
  },
  {
    "Key": "Rank A",
    "Value": [
      {
        "SubGroup": "hammers",
        "Model": "A",
        "Price": "130"
      },
      {
        "SubGroup": "hammers",
        "Model": "B",
        "Price": "142"
      },
      {
        "SubGroup": "hammers",
        "Model": "C",
        "Price": "185"
      },
      
    ]
  },
  {
    "Key": "Rank B",
    "Value": [
      {
        "SubGroup": "pliers",
        "Model": "A",
        "Price": "95"
      },
      {
        "SubGroup": "pliers",
        "Model": "B",
        "Price": "59"
      },
      {
        "SubGroup": "pliers",
        "Model": "C",
        "Price": "65"
      },
      
    ]
  },
  {
    "Key": "Rank C",
    "Value": [
      {
        "SubGroup": "saws",
        "Model": "A",
        "Price": "905"
      },
      {
        "SubGroup": "saws",
        "Model": "B",
        "Price": "589"
      },
      {
        "SubGroup": "saws",
        "Model": "C",
        "Price": "655"
      },
      
    ]
  },
  
]

答案1

得分: 2

添加额外的订单。首先用于检测特定项目,然后按照其余项目进行排序。
英文:

Add additional Order. First for detecting specific items, ThenBy for the rest.

var productList = CustomerData.Data.Products
    .GroupBy(x => x.CategoryDesc)
    .OrderByDescending(x => x.Key == "SPECIFIC")
    .ThenBy(x => x.Key)
    .ToDictionary(x => x.Key, x => x.ToList());

huangapple
  • 本文由 发表于 2023年2月19日 18:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定