linq中的分組操作符
分組是根據(jù)一個特定的值將序列中的元素進行分組。LINQ只包含一個分組操作符:GroupBy。GroupBy操作符類似于T-SQL語言中的Group By語句。來看看GroupBy的方法定義:
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector); public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer);
從方法定義中可以看出:GroupBy的返回值類型是:IEnumerable<IGrouping<TKey, TSource>>。其元素類型是IGrouping<TKey, TSource>。TKey屬性代表了分組時使用的關(guān)鍵值,TSource屬性代表了分組之后的元素集合。遍歷IGrouping<TKey, TSource>元素可以讀取到每一個TSource類型。看下面的例子:
1、定義Product類,其定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { public class Product { public int Id { get; set; } public int CategoryId { get; set; } public string Name { get; set; } public double Price { get; set; } public DateTime CreateTime { get; set; } } }
2、在Main()方法中調(diào)用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { class Program { static void Main(string[] args) { List<Product> listProduct = new List<Product>() { new Product(){Id=1,CategoryId=1, Name="C#高級編程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活著", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等數(shù)學", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="國家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 查詢表達式 var listExpress = from p in listProduct group p by p.CategoryId; Console.WriteLine("輸出查詢表達式結(jié)果"); foreach (var item in listExpress) { Console.WriteLine($"CategoryId:{item.Key}"); foreach(var p in item) { Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}"); } } Console.WriteLine("***************************************"); // 查詢方法 var listFun = listProduct.GroupBy(p => p.CategoryId); Console.WriteLine("輸出方法語法結(jié)果"); foreach (var item in listFun) { Console.WriteLine($"CategoryId:{item.Key}"); foreach (var p in item) { Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}"); } } Console.ReadKey(); } } }
結(jié)果:
下面在來看看多個分組條件的例子。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { class Program { static void Main(string[] args) { List<Product> listProduct = new List<Product>() { new Product(){Id=1,CategoryId=1, Name="C#高級編程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活著", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等數(shù)學", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="國家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 查詢表達式 var list = from p in listProduct group p by new { p.CategoryId, p.Price }; Console.WriteLine("查詢表達式方式1輸出:"); foreach (var item in list) { Console.WriteLine("key:" + item.Key); foreach (var subItem in item) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } var listExpress = from p in listProduct group p by new { p.CategoryId, p.Price } into r // 使用into把數(shù)據(jù)填充到局部變量r中,然后select篩選數(shù)據(jù) select new { key = r.Key, ListGroup = r.ToList() }; Console.WriteLine("查詢表達式方式2輸出:"); foreach(var item in listExpress) { Console.WriteLine("key:"+item.key); foreach (var subItem in item.ListGroup) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } // 方法語法 var listFun = listProduct.GroupBy(p => new { p.CategoryId, p.Price }).Select(g => new { key = g.Key, ListGroup = g.ToList() }); Console.WriteLine("方法語法輸出:"); foreach (var item in listFun) { Console.WriteLine("key:" + item.key); foreach (var subItem in item.ListGroup) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } Console.ReadKey(); } } }
結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#基礎(chǔ)之數(shù)據(jù)類型轉(zhuǎn)換
簡單認識顯式轉(zhuǎn)換和隱式轉(zhuǎn)換 我們就從下面這段代碼段開始吧2013-02-02asp.net 日期函數(shù) 某月的第一天和最后一天的日期
常用asp.net日期操作函數(shù)-得到某月的第一天和最后一天的日期2008-12-12aspx中的mysql操作類sqldatasource使用示例分享
服務器裝了mysql odbc驅(qū)動,想在那個iis上操作另一個服務器的mysql,找到個.net的sqldatasource類可以操作mysql,下在把使用方法分享一下2014-01-01ASP.NET Core AutoWrapper 自定義響應輸出實現(xiàn)
這篇文章主要介紹了ASP.NET Core AutoWrapper 自定義響應輸出實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08.NET 6實現(xiàn)基于JWT的Identity功能方法詳解
在.NET Web API開發(fā)中還有一個很重要的需求是關(guān)于身份認證和授權(quán)的。本文將介紹使用.NET框架自帶的認證和授權(quán)中間件去實現(xiàn)基于JWT的身份認證和授權(quán)功能的方法詳解,需要的可以參考一下2022-01-01asp.net連接數(shù)據(jù)庫 增加,修改,刪除,查詢代碼
asp.net連接數(shù)據(jù)庫,實現(xiàn)增加,修改,刪除,查詢的四大功能完整代碼。2009-07-07asp.net DataList與Repeater用法區(qū)別
Repeater比DataList要好一些,如果不是很大數(shù)據(jù)量的話,這點差別是體現(xiàn)不來的。2009-12-12