我没有广泛使用 LINQ,但是我使用它的次数越多,我就越意识到它的强大功能。如果您想从有界项的属性中排序,使用 LinqDataSource.OrderBy 子句显然很容易,但如果您想根据方法返回对项进行排序怎么办?以这门课为例(请忽略古怪的设计——它只是用来强调我的观点):
public class DataItem
{
private string Id { get; set; }
private int SortValue { get; set; }
public DataItem(string id, int sortValue)
{
this.Id = id;
this.SortValue = sortValue;
}
public int GetSortValue()
{
return SortValue;
}
}
有没有一种方法可以在 LinqDataSource 上设置 orderby 表达式,以便它使用从 GetSortValue 返回的值(即按属性以外的其他成员排序)但不改变 DataItem 类?