如何对内部List <myobject> </myobject>进行排序

时间:2012-08-28 20:21:09

标签: linq

我想对下面的'周'进行排序。 'InvalidCast Exception'告诉我,我甚至不接近(无论我抛出什么)。

sortedWeeks在调试器中看起来不错......

如何让它回到课堂上?

我实际上并不需要原来的Weeks类,因为它将通过JavaScriptSerializer运行......虽然我想知道如何对这个类进行排序......

Weeks theData = new Weeks();
// pretend its now full of data and I need the weeks sorted on number
var sortedWeeks = theData.weeks.OrderBy(x => x.number);

theData.weeks = sortedWeeks;  // <--- How do I cast this??

public class Weeks
{
     private List<Week> _weeks = new List<Week>();
     public List<Week> weeks
     {
         get { return _weeks; }
         set { _weeks = value; }
      }
}
public class Week
{
     public string week { get; set; }
     public string file { get; set; }
     public int number { get; set; }
 }

2 个答案:

答案 0 :(得分:5)

theData.weeks = sortedWeeks.ToList();

答案 1 :(得分:1)

theData.weeks.OrderBy(x =&gt; x.number);将返回IEnumerable<Week>IQueryable<Week>您需要ToList()来关闭查询并为您提供一个列表。另一种方法是将Weeks属性设为IEnumerable<Week>