我有一组对象,我想按它们的 name 属性按字母顺序排序。我尝试了以下方法:
List<Item> itemsToSort = dataProvider.Items.ToList();
List<Item> sortedItems = itemsToSort.OrderBy(x=>x.Name).ToList();
哪个不起作用。这些项目仍然以与以前相同的方式列出,而不是按字母顺序列出。
编辑
这是一个更完整的示例:
班级:
public class MeasureStation
{
#region Properties
public int ID
{
get { return _measureStation.ID; }
set { _measureStation.ID = value; }
}
[Required(ErrorMessage = "Navn skal udfyldes")]
public String Name
{
get { return _measureStation.Name; }
set { _measureStation.Name = value; }
}
public DateTime? DateEstablished
{
get { return _measureStation.DateEstablished; }
set { _measureStation.DateEstablished = value; }
}
public DateTime? OperationPeriodStart
{
get { return _measureStation.OperationPeriodStart; }
set { _measureStation.OperationPeriodStart = value; }
}
.
.
and so on...
}
查询是:
measureStations = dataProvider.MeasureStations.ToList();
var orderedMeasureStations = measureStations.OrderBy(x => x.Name);
查看 orderedMeasureStations 查询结果时,如下所示: 条目开头为:
F...
S...
a...
L...
所以它显然不是按名称排序的。