LINQ的语句来遍历集合,并确保每个项目是正确的顺序? [英] Linq statement to iterate through a collection and making sure each item is in the correct order?

查看:104
本文介绍了LINQ的语句来遍历集合,并确保每个项目是正确的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我我有一个简单的的IEnumerable<富> FOOS; 这个系列的一些结果从DB调用

i'm got a simple IEnumerable<foo> foos; This collection are some results from a db call.

我想,以确保列表中的每一项正确排序。数据库或者通过

I wish to make sure that each item in the list is ordered correctly. The Db either returns the results by

  • 将编号排序,最低到最高。例如。 1,2,3,4,。 ...等。
  • 在订购创建日期,减量(最近一次,第一次)。

这个问题能有一个快速的LINQ语句来完成的。要检查这个?

Can this be done with a quick linq statement .. to check for this?

如。 Assert.IsTrue(..在这里插入LINQ语句。)

目前,我有一个for循环,记住了previous项,如果检查的值,如果有一个值。这种感觉......麻烦了。

currently, i've got a for loop and remember the previous entry and check if the value of that, if there was a value. This feels ... cumbersome.

任何想法?

推荐答案

您可以将您的收藏比较有序的版本:

You could compare your collection to an ordered version:

CollectionAssert.AreEqual(foos.ToList(), foos.OrderBy(f => f.Id));

或者

CollectionAssert.AreEqual(foos.ToList(), 
                          foos.OrderByDescending(f => f.DateCreated));

这篇关于LINQ的语句来遍历集合,并确保每个项目是正确的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆