用于验证列表顺序是否正确的 lambda 表达式

发布于 2024-07-17 08:45:19 字数 469 浏览 3 评论 0原文

我想编写一个 lambda 表达式来验证列表的排序是否正确。 我有一个列表,其中一个人有一个 Name 属性,例如:

IList<Person> people = new List<Person>();
people.Add(new Person(){ Name = "Alan"});
people.Add(new Person(){ Name = "Bob"});
people.Add(new Person(){ Name = "Chris"});

我正在尝试测试该列表是否按 Name 属性按 ASC 排序。所以我正在寻找类似

Assert.That(people.All(....), "list of person not ordered correctly");

如何编写 lambda 来检查列表中的每个人列表中的名字小于列表中下一个人的名字?

I want to write a lambda expression to verify that a list is ordered correctly. I have a List where a person has a Name property eg:

IList<Person> people = new List<Person>();
people.Add(new Person(){ Name = "Alan"});
people.Add(new Person(){ Name = "Bob"});
people.Add(new Person(){ Name = "Chris"});

I'm trying to test that the list is ordered ASC by the Name property.So I'm after something like

Assert.That(people.All(....), "list of person not ordered correctly");

How can I write a lambda to check that each Person in the list has a name less that the next person in the list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

盗琴音 2024-07-24 08:45:19

这是 Jared 解决方案的替代方案 - 它几乎相同,但使用 foreach 循环和布尔变量来检查这是否是第一次迭代。 我通常发现这比手动迭代更容易:

public static bool IsOrdered<T>(this IEnumerable<T> source)
{
  var comparer = Comparer<T>.Default;
  T previous = default(T);
  bool first = true;

  foreach (T element in source)
  {
      if (!first && comparer.Compare(previous, element) > 0)
      {
          return false;
      }
      first = false;
      previous = element;
  }
  return true;
}

Here's an alternative to Jared's solution - it's pretty much the same, but using a foreach loop and a Boolean variable to check whether or not this is the first iteration. I usually find that easier than iterating manually:

public static bool IsOrdered<T>(this IEnumerable<T> source)
{
  var comparer = Comparer<T>.Default;
  T previous = default(T);
  bool first = true;

  foreach (T element in source)
  {
      if (!first && comparer.Compare(previous, element) > 0)
      {
          return false;
      }
      first = false;
      previous = element;
  }
  return true;
}
不必在意 2024-07-24 08:45:19

我不相信目前有任何 LINQ 运算符可以涵盖这种情况。 但是,您可以编写一个 IsOrdered 方法来完成这项工作。 例如。

public static bool IsOrdered<T>(this IEnumerable<T> enumerable) {
  var comparer = Comparer<T>.Default;
  using ( var e = enumerable.GetEnumerator() ) {
    if ( !e.MoveNext() ) {
      return true;
    }
    var previous = e.Current;
    while (e.MoveNext()) {
      if ( comparer.Compare(previous, e.Current) > 0) {
        return false;
      }
      previous = e.Current;
    }
    return true;
  }
}

然后您可以使用以下内容来验证您的列表:

var isOrdered = people.Select(x => x.Name).IsOrdered();

I don't believe there is any LINQ operator which currently covers this case. However you could write an IsOrdered method which does the work. For example.

public static bool IsOrdered<T>(this IEnumerable<T> enumerable) {
  var comparer = Comparer<T>.Default;
  using ( var e = enumerable.GetEnumerator() ) {
    if ( !e.MoveNext() ) {
      return true;
    }
    var previous = e.Current;
    while (e.MoveNext()) {
      if ( comparer.Compare(previous, e.Current) > 0) {
        return false;
      }
      previous = e.Current;
    }
    return true;
  }
}

Then you could use the following to verify your list:

var isOrdered = people.Select(x => x.Name).IsOrdered();
蓝天白云 2024-07-24 08:45:19

我知道这是一个老问题,但我使用 Linq 有一个非常好的解决方案:

people.Zip(people.OrderBy(p => p.Name), (a, b) => a == b).All(eq => eq);

基本上,您将序列与有序序列合并,并投影一个 bool 值来指示两个条目是否相等:

"Alan"  -- "Alan"  => true
"Bob"   -- "Bob"   => true
"Chris" -- "Chris" => true

然后使用 All 方法询问集合中的所有项目是否为true

I know this is an old question, but I have a very nice solution for this using Linq:

people.Zip(people.OrderBy(p => p.Name), (a, b) => a == b).All(eq => eq);

Basically, you are merging a sequence with the ordered sequence, and projecting a bool value indicating whether both entries are equal:

"Alan"  -- "Alan"  => true
"Bob"   -- "Bob"   => true
"Chris" -- "Chris" => true

Then with the All method you ask if all the items in the collection are true.

逆夏时光 2024-07-24 08:45:19

怎么样:

people.SequenceEqual( people.OrderBy( x=>x.Name ) );

SequenceEqual() 从 3.5 开始可用
如果您想确认没有重复项,可以在 OrderBy() 之后添加 Distinct()。

What about:

people.SequenceEqual( people.OrderBy( x=>x.Name ) );

SequenceEqual() has been available since 3.5
You can add Distinct() after OrderBy() if you want to confirm there are no duplicates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文