另一个 LINQ OrderBy 查询

发布于 2024-09-25 00:59:24 字数 1351 浏览 1 评论 0原文

我有一个通过一系列 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2024-10-02 00:59:24

我相信这会满足您的要求:

var order = data
    .Where(x => x.RowLabel == "All")
    .OrderByDescending(x => x.Value)
    .Select((x, i) => new { x, i })
    .ToDictionary(x => x.x.ColumnLabel, x => x.i);
var result = data
    .GroupBy(x => x.RowLabel, (x, y) => y.OrderBy(z => order[z.ColumnLabel]));

foreach (var x in result)
{
    foreach (var y in x)
    {
        Console.WriteLine("ColumnLabel: {0}, RowLabel: {1}, Value: {2}", y.ColumnLabel, y.RowLabel, y.Value);                    
    }
}

结果:

ColumnLabel: Dog, RowLabel: All, Value: 17 
ColumnLabel: Cat, RowLabel: All, Value: 9 
ColumnLabel: Fish, RowLabel: All, Value: 4
ColumnLabel: Dog, RowLabel: Girls, Value: 2
ColumnLabel: Cat, RowLabel: Girls, Value: 3
ColumnLabel: Fish, RowLabel: Girls, Value: 2
ColumnLabel: Dog, RowLabel: Boys, Value: 15
ColumnLabel: Cat, RowLabel: Boys, Value: 6
ColumnLabel: Fish, RowLabel: Boys, Value: 2

如果我误解了您的问题,请告诉我,但这里的想法是创建一个字典order,为后续的分组查询提供排序。另一个关键思想是使用 group-by 的 resultSelector 对其中的元素进行排序(因为它们代表列)。

I believe this will do what you want:

var order = data
    .Where(x => x.RowLabel == "All")
    .OrderByDescending(x => x.Value)
    .Select((x, i) => new { x, i })
    .ToDictionary(x => x.x.ColumnLabel, x => x.i);
var result = data
    .GroupBy(x => x.RowLabel, (x, y) => y.OrderBy(z => order[z.ColumnLabel]));

foreach (var x in result)
{
    foreach (var y in x)
    {
        Console.WriteLine("ColumnLabel: {0}, RowLabel: {1}, Value: {2}", y.ColumnLabel, y.RowLabel, y.Value);                    
    }
}

Result:

ColumnLabel: Dog, RowLabel: All, Value: 17 
ColumnLabel: Cat, RowLabel: All, Value: 9 
ColumnLabel: Fish, RowLabel: All, Value: 4
ColumnLabel: Dog, RowLabel: Girls, Value: 2
ColumnLabel: Cat, RowLabel: Girls, Value: 3
ColumnLabel: Fish, RowLabel: Girls, Value: 2
ColumnLabel: Dog, RowLabel: Boys, Value: 15
ColumnLabel: Cat, RowLabel: Boys, Value: 6
ColumnLabel: Fish, RowLabel: Boys, Value: 2

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).

生生漫 2024-10-02 00:59:24

经过思考,这就是我想到的。它或多或少与柯克的相同,但不使用字典。我的目标是通过一个查询来完成此操作,(select ... into) 确实救了我。

var query = from item in data
            where item.RowLabel == "All"
            orderby item.Value descending
            select item.ColumnLabel into columnOrder
            join item in data on columnOrder equals item.ColumnLabel
            group item by item.RowLabel;

var lquery = data.Where(item => item.RowLabel == "All")
                 .OrderByDescending(item => item.Value)
                 .Select(item => item.ColumnLabel)
                 .Join(data, columnOrder => columnOrder,
                             item => item.ColumnLabel,
                             (columnOrder, item) => item)
                 .GroupBy(item => item.RowLabel);

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.

var query = from item in data
            where item.RowLabel == "All"
            orderby item.Value descending
            select item.ColumnLabel into columnOrder
            join item in data on columnOrder equals item.ColumnLabel
            group item by item.RowLabel;

var lquery = data.Where(item => item.RowLabel == "All")
                 .OrderByDescending(item => item.Value)
                 .Select(item => item.ColumnLabel)
                 .Join(data, columnOrder => columnOrder,
                             item => item.ColumnLabel,
                             (columnOrder, item) => item)
                 .GroupBy(item => item.RowLabel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文