最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linq中的分組操作符

 更新時(shí)間:2022年03月10日 09:42:49   作者:.NET開發(fā)菜鳥  
這篇文章介紹了linq中的分組操作符,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

分組是根據(jù)一個(gè)特定的值將序列中的元素進(jìn)行分組。LINQ只包含一個(gè)分組操作符:GroupBy。GroupBy操作符類似于T-SQL語(yǔ)言中的Group By語(yǔ)句。來(lái)看看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屬性代表了分組時(shí)使用的關(guān)鍵值,TSource屬性代表了分組之后的元素集合。遍歷IGrouping<TKey, TSource>元素可以讀取到每一個(gè)TSource類型??聪旅娴睦樱?/p>

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#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運(yùn)維", 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ù)學(xué)", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="國(guó)家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 查詢表達(dá)式
            var listExpress = from p in listProduct group p by p.CategoryId;
            Console.WriteLine("輸出查詢表達(dá)式結(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("輸出方法語(yǔ)法結(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é)果:

 下面在來(lái)看看多個(gè)分組條件的例子。

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#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運(yùn)維", 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ù)學(xué)", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="國(guó)家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 查詢表達(dá)式
            var list = from p in listProduct group p by new { p.CategoryId, p.Price };
            Console.WriteLine("查詢表達(dá)式方式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("查詢表達(dá)式方式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}");
                }
            }

            // 方法語(yǔ)法
            var listFun = listProduct.GroupBy(p => new { p.CategoryId, p.Price }).Select(g => new { key = g.Key, ListGroup = g.ToList() });
            Console.WriteLine("方法語(yǔ)法輸出:");
            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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 創(chuàng)建基于ASP.NET的SMTP郵件服務(wù)的具體方法

    創(chuàng)建基于ASP.NET的SMTP郵件服務(wù)的具體方法

    Asp.net在System.Web.Mail名稱空間中有一個(gè)發(fā)送email的內(nèi)建類,但這僅是cdosys的一個(gè)假象。開發(fā)者能使用一個(gè)替代的它smtp郵件服務(wù)。在這篇文章里面,我將會(huì)展示如何創(chuàng)建一個(gè)用于asp.net的功能齊全的smtp郵件服務(wù)
    2013-11-11
  • 詳解.NET Core中的Worker Service

    詳解.NET Core中的Worker Service

    這篇文章主要介紹了.NET Core中的Worker Service的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • ASP.NET Core中的配置詳解

    ASP.NET Core中的配置詳解

    這篇文章主要介紹了ASP.NET Core中的配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • asp.net LINQ中數(shù)據(jù)庫(kù)連接字符串的問題

    asp.net LINQ中數(shù)據(jù)庫(kù)連接字符串的問題

    這兩天一直在用LINQ做開發(fā),也是第一次嘗試用LINQ做開發(fā),效率沒的說(shuō),開發(fā)過(guò)程中遇到一個(gè)問題困擾了我好久,今天問題終于解決了,發(fā)上來(lái)和大家分享一下,也給自己做個(gè)備忘。
    2010-03-03
  • .NET 與樹莓派WS28XX 燈帶的顏色漸變動(dòng)畫效果的實(shí)現(xiàn)

    .NET 與樹莓派WS28XX 燈帶的顏色漸變動(dòng)畫效果的實(shí)現(xiàn)

    所謂顏色漸變動(dòng)畫,首先,你要確定兩種顏色——起始色和最終色,比如從綠色變成紅色,綠色是起始,紅色是終點(diǎn)。這篇文章主要介紹了.NET 與樹莓派WS28XX 燈帶的顏色漸變動(dòng)畫,需要的朋友可以參考下
    2021-12-12
  • 未能加載文件或程序集“XXX”或它的某一個(gè)依賴項(xiàng)。試圖加載格式不正確的程序。

    未能加載文件或程序集“XXX”或它的某一個(gè)依賴項(xiàng)。試圖加載格式不正確的程序。

    如果你將應(yīng)用程序生成x86而不是Any CPU時(shí),在64位操作系統(tǒng)中不會(huì)出錯(cuò)錯(cuò)誤,而在32位操作系統(tǒng)中可能會(huì)出現(xiàn)以下錯(cuò)誤
    2012-11-11
  • 國(guó)產(chǎn)化之銀河麒麟安裝.netcore3.1的詳細(xì)步驟(手動(dòng)安裝)

    國(guó)產(chǎn)化之銀河麒麟安裝.netcore3.1的詳細(xì)步驟(手動(dòng)安裝)

    這篇文章主要介紹了國(guó)產(chǎn)化之銀河麒麟安裝.netcore3.1的詳細(xì)步驟(手動(dòng)安裝),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • asp.net部署到IIS常見問題的解決方法

    asp.net部署到IIS常見問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了asp.net部署到IIS常見問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • ASP.Net項(xiàng)目中實(shí)現(xiàn)微信APP支付功能

    ASP.Net項(xiàng)目中實(shí)現(xiàn)微信APP支付功能

    這篇文章介紹了ASP.Net項(xiàng)目中實(shí)現(xiàn)微信APP支付功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#反射的一些應(yīng)用

    C#反射的一些應(yīng)用

    初始聽說(shuō)反射是可以動(dòng)態(tài)的調(diào)用程序集,并從中來(lái)獲取相應(yīng)的方法和屬性,感覺比較神奇,,,
    2013-02-02

最新評(píng)論

龙岩市| 哈尔滨市| 兴海县| 忻城县| 沁水县| 疏附县| 颍上县| 浦城县| 阜阳市| 敦化市| 来安县| 平原县| 永泰县| 仪征市| 宽甸| 图木舒克市| 且末县| 桐梓县| 定日县| 当雄县| 会同县| 浦城县| 尼勒克县| 奎屯市| 芦溪县| 盘山县| 安塞县| 肥城市| 叶城县| 铜鼓县| 威信县| 微山县| 普洱| 淅川县| 遵化市| 龙山县| 吕梁市| 湖南省| 舟曲县| 泌阳县| 兴仁县|