版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第LINQ排序操作符用法Linq中的排序操作符包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
一、OrderBy操作符
OrderBy操作符用于對輸入序列中的元素進(jìn)行排序,排序基于一個委托方法的返回值順序。排序過程完成后,會返回一個類型為IOrderEnumerableT的集合對象。其中IOrderEnumerableT接口繼承自IEnumerableT接口。下面來看看OrderBy的定義:
從上面的截圖中可以看出,OrderBy是一個擴(kuò)展方法,只要實現(xiàn)了IEnumerableT接口的就可以使用OrderBy進(jìn)行排序。OrderBy共有兩個重載方法:第一個重載的參數(shù)是一個委托類型和一個實現(xiàn)了IComparerT接口的類型。第二個重載的參數(shù)是一個委托類型??纯聪旅娴氖纠?/p>
定義產(chǎn)品類:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
publicclassProducts
publicintId{get;set;}
publicintCategoryId{get;set;}
publicstringName{get;set;}
publicdoublePrice{get;set;}
publicDateTimeCreateTime{get;set;}
}
在Main()方法里面調(diào)用:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=1,Name="ASP.NETCore",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=1,Name="EntityFramework6.x",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
Console.WriteLine("方法語法");
//1、查詢方法,返回匿名類
varlist=listProduct.OrderBy(p=p.CreateTime).Select(p=new{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.WriteLine("查詢表達(dá)式");
//2、查詢表達(dá)式,返回匿名類
varlistExpress=frompinlistProductorderbyp.CreateTimeselectnew{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime};
foreach(variteminlistExpress)
Console.WriteLine($"item:{item}");
Console.ReadKey();
}
結(jié)果:
從截圖中可以看出,集合按照CreateTime進(jìn)行升序排序。
在來看看第一個重載方法的實現(xiàn):
先定義PriceComparer類實現(xiàn)IComparerT接口,PriceComparer類定義如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
publicclassPriceComparer:IComparerdouble
publicintCompare(doublex,doubley)
if(xy)
return1;//表示xy
elseif(xy)
return-1;//表示xy
else
return0;//表示x=y
}
在Main()方法里面調(diào)用:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=1,Name="ASP.NETCore",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=1,Name="EntityFramework6.x",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
Console.WriteLine("方法語法");
//1、查詢方法,按照價格升序排序,返回匿名類
varlist=listProduct.OrderBy(p=p.Price,newPriceComparer()).Select(p=new{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.ReadKey();
}
結(jié)果:
注意:orderby必須在select之前出現(xiàn),查詢表達(dá)式最后只可能出現(xiàn)select或者groupby。
二、OrderByDescending
OrderByDescending操作符的功能與OrderBy操作符基本相同,二者只是排序的方式不同。OrderBy是升序排序,而OrderByDescending則是降序排列。下面看看OrderByDescending的定義:
從方法定義中可以看出,OrderByDescending的方法重載和OrderBy的方法重載一致。來看下面的例子:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=1,Name="ASP.NETCore",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=1,Name="EntityFramework6.x",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
//注意:OrderByDescending的方法語法和查詢表達(dá)式寫法有些不同。
Console.WriteLine("方法語法");
//1、查詢方法,按照時間降序排序,返回匿名類
varlist=listProduct.OrderByDescending(p=p.CreateTime).Select(p=new{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.WriteLine("查詢表達(dá)式");
varlistExpress=frompinlistProductorderbyp.CreateTimedescendingselectnew{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime};
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.ReadKey();
}
結(jié)果:
從截圖中可以看出:輸出結(jié)果按照時間降序排序。在來看看另外一個重載方法的調(diào)用:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=1,Name="ASP.NETCore",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=1,Name="EntityFramework6.x",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
Console.WriteLine("方法語法");
//1、查詢方法,按照價格降序排序,返回匿名類
varlist=listProduct.OrderByDescending(p=p.Price,newPriceComparer()).Select(p=new{id=p.Id,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.ReadKey();
}
結(jié)果:
輸出結(jié)果也是按照時間降序排序。
三、ThenBy排序
ThenBy操作符可以對一個類型為IOrderedEnumerableT,(OrderBy和OrderByDesceding操作符的返回值類型)的序列再次按照特定的條件順序排序。ThenBy操作符實現(xiàn)按照次關(guān)鍵字對序列進(jìn)行升序排列。下面來看看ThenBy的定義:
從截圖中可以看出:ThenBy()方法擴(kuò)展的是IOrderedEnumerableT,因此ThenBy操作符長常常跟在OrderBy和OrderByDesceding之后??聪旅娴氖纠?/p>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=2,Name="活著",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=3,Name="高等數(shù)學(xué)",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
//注意:ThenBy()的方法語法和查詢表達(dá)式寫法有些不同。
Console.WriteLine("方法語法升序排序");
//1、查詢方法,按照商品分類升序排序,如果商品分類相同在按照價格升序排序返回匿名類
varlist=listProduct.OrderBy(p=p.CategoryId).ThenBy(p=p.Price).Select(p=new{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.WriteLine("查詢表達(dá)式升序排序");
varlistExpress=frompinlistProductorderbyp.CategoryId,p.Priceselectnew{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime};
foreach(variteminlistExpress)
Console.WriteLine($"item:{item}");
Console.WriteLine("方法語法降序排序");
//1、查詢方法,按照商品分類降序排序,如果商品分類相同在按照價格升序排序返回匿名類
varlistDesc=listProduct.OrderByDescending(p=p.CategoryId).ThenBy(p=p.Price).Select(p=new{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlistDesc)
Console.WriteLine($"item:{item}");
Console.WriteLine("查詢表達(dá)式降序排序");
varlistExpressDesc=frompinlistProductorderbyp.CategoryIddescending,p.Priceselectnew{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime};
foreach(variteminlistExpressDesc)
Console.WriteLine($"item:{item}");
Console.ReadKey();
}
結(jié)果:
四、ThenByDescending
ThenByDescending操作符于ThenBy操作符非常類似,只是是按照降序排序,實現(xiàn)按照次關(guān)鍵字對序列進(jìn)行降序排列。來看看ThenByDescending的定義:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceOrderOperation
classProgram
staticvoidMain(string[]args)
//初始化數(shù)據(jù)
ListProductslistProduct=newListProducts()
newProducts(){Id=1,CategoryId=1,Name="C#高級編程第10版",Price=100.67,CreateTime=DateTime.Now},
newProducts(){Id=2,CategoryId=1,Name="Redis開發(fā)和運維",Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
newProducts(){Id=3,CategoryId=2,Name="活著",Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
newProducts(){Id=4,CategoryId=3,Name="高等數(shù)學(xué)",Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
//注意:ThenByDescending()的方法語法和查詢表達(dá)式寫法有些不同。
Console.WriteLine("方法語法升序排序");
//1、查詢方法,按照商品分類升序排序,如果商品分類相同在按照價格降序排序返回匿名類
varlist=listProduct.OrderBy(p=p.CategoryId).ThenByDescending(p=p.Price).Select(p=new{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime}).ToList();
foreach(variteminlist)
Console.WriteLine($"item:{item}");
Console.WriteLine("查詢表達(dá)式升序排序");
varlistExpress=frompinlistProductorderbyp.CategoryId,p.Pricedescendingselectnew{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime};
foreach(variteminlistExpress)
Console.WriteLine($"item:{item}");
Console.WriteLine("方法語法降序排序");
//1、查詢方法,按照商品分類降序排序,如果商品分類相同在按照價格降序排序返回匿名類
varlistDesc=listProduct.OrderByDescending(p=p.CategoryId).ThenByDescending(p=p.Price).Select(p=new{id=p.CategoryId,ProductName=p.Name,ProductPrice=p.Price,Pub
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 道路施工現(xiàn)場管理及驗收方案
- 燃?xì)庠O(shè)施應(yīng)急響應(yīng)演練方案
- 大口徑管道安裝技術(shù)方案
- 婦幼保健院患者轉(zhuǎn)診流程優(yōu)化方案
- 通信網(wǎng)絡(luò)優(yōu)化方案設(shè)計與實施指南(標(biāo)準(zhǔn)版)
- 成本實操-物業(yè)管理公司成本管理控制方案 SOP
- 新疆工業(yè)職業(yè)技術(shù)學(xué)院《組織行為學(xué)(工商)》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣元中核職業(yè)技術(shù)學(xué)院《電磁場的數(shù)值方法》2023-2024學(xué)年第二學(xué)期期末試卷
- 新疆科技職業(yè)技術(shù)學(xué)院《簡筆畫與英語書法》2023-2024學(xué)年第二學(xué)期期末試卷
- 興義民族師范學(xué)院《社會科學(xué)基礎(chǔ)》2023-2024學(xué)年第二學(xué)期期末試卷
- T-CCTAS 237-2025 城市軌道交通市域快線車輛運營技術(shù)規(guī)范
- 園林環(huán)衛(wèi)安全培訓(xùn)內(nèi)容課件
- 軟件系統(tǒng)上線測試與驗收報告
- 冬季交通安全測試題及答案解析
- 2025年國家能源局系統(tǒng)公務(wù)員面試模擬題及備考指南
- (2025年標(biāo)準(zhǔn))圈內(nèi)認(rèn)主協(xié)議書
- 2025年安徽省中考化學(xué)真題及答案
- 2025年軍隊文職人員統(tǒng)一招聘面試( 臨床醫(yī)學(xué))題庫附答案
- 海馬體核磁掃描課件
- 某電力股份企業(yè)同熱三期2×100萬千瓦項目環(huán)評報告書
- 2026屆上海市部分區(qū)中考一模語文試題含解析
評論
0/150
提交評論