英文:
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());
它工作得很好。
我想要一个特定的组(带有销售)排在列表的第一位。
但其他组应按降序排列。
类似这样:
***我以这种格式写是为了方便查看
[
{
"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:
***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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论