【问题标题】:Lambda Sort asc [closed]Lambda 排序 asc [关闭]
【发布时间】:2014-08-08 20:32:06
【问题描述】:

我是 lambda 表达式的新手,在弄清楚如何按升序排序时遇到了一些麻烦。

我目前拥有的代码只是按字母顺序对我的 X 值进行排序。我想按升序对 Y 值进行排序。

我知道我需要在某个地方使用Count(),但我不知道在哪里。

下面是我的代码:

var query = PIList
    .OrderByDescending(x => x.Failcode)
    .ThenByDescending(x => x.Failcode)
    .Select(x => x.Failcode)
    .Distinct();

foreach (var value in query)
{
    PIFailCodeChart.Series[0].Points
        .AddXY(value, PIList.Where(x => x.Failcode == value).Count());
}

你能告诉我我做错了什么吗?

【问题讨论】:

  • 谢谢,@Eddie_cat。不过,你打败了我。哈哈
  • 您两次都按Failcode 排序...这有什么用? Failcode 是 X 值还是 Y 值?您需要在此处提供有关您的数据的更多信息...(如果您只是向图表添加值,为什么顺序很重要?)
  • 相当肯定 OrderBy 默认会升序。
  • 作为新用户,什么都不会发生。它的社区告诉你,你需要解决你的问题,因为它要么不清楚,要么没有得到很好的研究。不要冒犯......就像@Pheonix说的那样,下次改进你的问题。

标签: c# linq lambda


【解决方案1】:

Here's a .NET Fiddle showing how 这是代码:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<string> s = new List<string>() { "a", "b", "c", "a"};
        var asc = s.OrderBy(e => e).ToList<string>();
        Console.WriteLine(string.Join(", ", asc));
    }
}

这会输出a, a, b, c,一个升序排序的列表。

你可以这样做:

var query = PIList.Distinct(x => x.FailCode)
    .OrderBy(x => x.FailCode)
    .Select(x => x.FailCode);

【讨论】:

  • 注意OrderBy 而不是OrderByDescending
  • 当我第一次学习 LINQ 时,这确实让我感到困惑。这有点不一致。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 2017-10-27
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多