动态排序 不过不完善
排序class
1
public class OrderBy2

{ 3
List<post> posts = new List<post>();4

5
public List<post > GtePost()6

{7

8
for (int i = 0, j=1000; i < 1000 ; i++, j--)9

{10
post p = new post();11
p.Ty = i;12
p.Uu=j;13
p.UserName = "1";14
posts.Add(p);15
}16
return posts;17

18
}19
20

/**//// <summary>21
/// Func<post, TKey> 委托的实现22
/// </summar排序的列y>23
/// <param name="p"></param>24
/// <returns> 可以动态设置排序的列</returns>25
int testSelector(post p)26

{27
return p.Uu;28
}29
public IEnumerable<post> GetData()30

{31
GtePost();32
return Sort<int>("", testSelector);33
}34

35
public IEnumerable<post> Sort<TKey>(string userName, Func<post, TKey> selector)36

{37
return from post in posts38
where post.UserName == userName39
orderby selector(post) descending40
select post;41
}42
}UI:
1
void BindData()2

{3
4
OrderBy o = new OrderBy(); 5
6
post p=new post ();7

8
this.dataGridView1.DataSource = o.GetData().ToList<post>();9
10
11
}遗留问题:
排序对排序字段类型的依赖?委托函数的返回值 int testSelector(post p) 受返回类型的依赖 不能动态的设置返回类型吧