新聞中心
linq動態(tài)排序不是很好實現(xiàn)的,很多開發(fā)者都在這個方面有疑惑,所以筆者今天分享一下自己的經(jīng)驗,希望能給大家?guī)韼椭?/p>

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供瀘縣網(wǎng)站建設(shè)、瀘縣做網(wǎng)站、瀘縣網(wǎng)站設(shè)計、瀘縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、瀘縣企業(yè)網(wǎng)站模板建站服務(wù),10年瀘縣做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
想實現(xiàn)linq動態(tài)排序就是linq的order by 后跟動態(tài)的函數(shù)可以用委托實現(xiàn),但是發(fā)現(xiàn)實現(xiàn)委托函數(shù)時候:
- int testSelector(post p)
- {
- ret urn p.Uu;
- }
這個返回類型受到了限制僅是相同類型的字段可以動態(tài)排序是不是Func selector 這個的實現(xiàn)我理解的有問題 ?
- public IEnumerable Sort ( string userName, Func
- selector)
- {
- return from post in posts
- where post.UserName == userName
- orderby selector(post) descending
- select post;
- }
這個函數(shù)的TKey限制只能是一種類型 ?有什么好的實現(xiàn)嗎?
linq動態(tài)排序具體解決的代碼
- public class OrderBy
- {
- List posts = new List ();
- public List GtePost()
- {
- for (int i = 0, j=1000; i < 1000 ; i++, j--)
- {
- post p = new post();
- p.Ty = i;
- p.Uu=j;
- p.UserName = "1";
- posts.Add(p);
- }
- return posts;
- }
- /**////
- /// Func 委托的實現(xiàn)
- ///
- ///
- /// 可以動態(tài)設(shè)置排序的列
- int testSelector(post p)
- {
- return p.Uu;
- }
- public IEnumerable GetData()
- {
- GtePost();
- return Sort
("", testSelector); - }
- public IEnumerable Sort ( string userName, Func
- TKey> selector)
- {
- return from post in posts
- where post.UserName == userName
- orderby selector(post) descending
- select post;
- }
- }
linq動態(tài)排序Code
- void BindData()
- {
- OrderBy o = new OrderBy();
- post p=new post ();
- this.dataGridView1.DataSource = o.GetData().ToList ();
- }
以上就是對linq動態(tài)排序解決方法的簡單介紹。
網(wǎng)頁題目:詳細闡述linq動態(tài)排序
轉(zhuǎn)載來源:http://fisionsoft.com.cn/article/dpdhish.html


咨詢
建站咨詢
