另一个 LINQ OrderBy 查询
我有一个通过一系列 linq 查询重新创建的表(如数字列表)。我试图根据第一行中的值按降序对列进行排序。
我的数据库看起来像这样:
Data
{
ColumnLabel,
RowLabel,
Value
}
{"Cat", "All", 9}
{"Dog","All", 17}
{"Fish", "All", 4}
{"Cat", "Girls", 3}
{"Dog","Girls", 2}
{"Fish", "Girls", 2}
{"Cat", "Boys", 6}
{"Dog","Boys", 15}
{"Fish", "Boys", 2}
当我天真地将这个表连接在一起时,它看起来像这样:
Cat | Dog | Fish
All 9 17 4
Girls 3 15 2
Boys 6 2 2
我想要做的是根据 Value
按降序对 ColumnLabels
进行排序,其中 RowLabel ==“全部”
。我希望表格看起来像这样:
Dog | Cat | Fish
All 17 9 4
Girls 15 3 2
Boys 2 6 2
注意狗和猫的行翻转了。
我需要使用 HTML 输出该表,以便逐行迭代。
我首先对 ColumnLabels 进行排序:
var top = data.Where(x => x.RowLabel == "All") .OrderBy(x => x.Data) .Select(x => x.ColumnLabel).SingleOrDefault();
现在 top
包含我希望列出现的顺序; {狗、猫、鱼}
。我真的不在乎行如何垂直排序。
然后我想要做的是根据每一行的 RowLabel
值对其进行分组:
var row = data.GroupBy(x => x.RowLabel)
.OrderBy(y => ??????????????);
//I want to OrderBy the values in top
//How do I do this?
然后我迭代每一行并创建我的 HTML 表。这部分很简单,但我不知道如何根据最高顺序对后续行进行排序。如何在 LINQ 中执行此操作?
I have a table (as in a list of numbers) that gets recreated through a series of linq queries. I'm trying to sort the columns in descending order based upon the values within the first row.
My Database looks like this:
Data
{
ColumnLabel,
RowLabel,
Value
}
{"Cat", "All", 9}
{"Dog","All", 17}
{"Fish", "All", 4}
{"Cat", "Girls", 3}
{"Dog","Girls", 2}
{"Fish", "Girls", 2}
{"Cat", "Boys", 6}
{"Dog","Boys", 15}
{"Fish", "Boys", 2}
When I naively join this table together it looks like this:
Cat | Dog | Fish
All 9 17 4
Girls 3 15 2
Boys 6 2 2
What I want to do is sort the ColumnLabels
in descending order based on Value
where RowLabel == "All"
. The way I WANT the table to look is like this:
Dog | Cat | Fish
All 17 9 4
Girls 15 3 2
Boys 2 6 2
Notice the Dog and Cat row flipped.
I'll need to out put this table using HTML so I iterate through by each row.
I start by sorting the ColumnLabels
:
var top = data.Where(x => x.RowLabel == "All")
.OrderBy(x => x.Data)
.Select(x => x.ColumnLabel).SingleOrDefault();
Now top
contains the order I want the Columns to appear; {Dog, Cat, Fish}
. I don't really care how the Rows are sorted vertically.
What I then want to do is group each row based upon it's RowLabel
value:
var row = data.GroupBy(x => x.RowLabel)
.OrderBy(y => ??????????????);
//I want to OrderBy the values in top
//How do I do this?
I then iterate through each row and create my HTML table. This part is easy, but I don't know how to order subsequent rows based upon the top order. How do I do this in LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这会满足您的要求:
结果:
如果我误解了您的问题,请告诉我,但这里的想法是创建一个字典
order
,为后续的分组查询提供排序。另一个关键思想是使用 group-by 的 resultSelector 对其中的元素进行排序(因为它们代表列)。I believe this will do what you want:
Result:
Let me know if I misinterpreted your question, but the idea here is to create a dictionary
order
that provides the ordering for the subsequent group-by query. The other key idea is to use the resultSelector of group-by to order the elements within (since those represent the columns).经过思考,这就是我想到的。它或多或少与柯克的相同,但不使用字典。我的目标是通过一个查询来完成此操作,(select ... into) 确实救了我。
After thinking about this, this is what I came up with. It's more or less the same as Kirk's but doesn't use a dictionary. I was aiming for a single query to do this, (select ... into) really saved me there.