• LINQ之GroupBy(group p by into)


    [C#] LINQ之GroupBy - cnxy - 博客园 (cnblogs.com)

    LINQ TO SQL 中的group - min.jiang - 博客园 (cnblogs.com)

    对查询结果进行分组(C# 中的 LINQ) | Microsoft Docs

    代码如下

    void Main()
    {
        List<Person> personList = new List<Person>
            {
                new Person
                {
                    Name = "P1", Age = 18, Gender = "Male"
    
                },
                new Person
                {
                    Name = "P2", Age = 19, Gender = "male",
                },
                new Person
                {
                    Name = "p2", Age = 17,Gender = "Female",
                }
                ,
                new Person
                {
                    Name = "p3", Age = 17,Gender = "Female",
                }
            };
    
    
        Console.WriteLine("********第一种用法*************************************");
        //public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组。
        var groups = personList.GroupBy(p => p.Gender);
    
        //    其等价的LINQ语句为:
        //var groups = from p in personList
        //             group p by p.Gender;
        foreach (var group in groups)
        {
            Console.WriteLine(group.Key);
            foreach (var person in group)
            {
                Console.WriteLine($"	{person.Name},{person.Age}");
            }
        }
    
        Console.WriteLine("********第二种用法*************************************");
        //public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组,并使用指定的比较器对键进行比较。
        var groups2 = personList.GroupBy(p => p.Gender, new PersonEqualityComparer());
        foreach (var group in groups2)
        {
            Console.WriteLine(group.Key.ToString());
            foreach (var person in group)
            {
                Console.WriteLine($"	{person.Name},{person.Age}");
            }
        }
        Console.WriteLine();
    
        Console.WriteLine("********第三种用法*************************************");
        //public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组,并且通过使用指定的函数对每个组中的元素进行投影。             
        var groups3 = personList.GroupBy(p => p.Gender, p => new { p.Age, p.Name });
        //其等价的LINQ语句为:
        //var groups3 = from p in personList
        //              group p.Gender by p.Name;
    
        foreach (var group in groups3)
        {
            Console.WriteLine(group.Key.ToString());
            foreach (var o in group)
            {
                Console.WriteLine($"	{o.Name},{o.Age}");
            }
        }
        Console.WriteLine();
    
        Console.WriteLine("********第四种用法*************************************");
        //public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值。
        string GetPersonInfo(string gender, IEnumerable<Person> persons)
        {
            string result = $"{gender}:	";
            foreach (var p in persons)
            {
                result += $"{p.Name},{p.Age}	";
            }
            return result;
        }
    
        var results4 = personList.GroupBy(p => p.Gender, (g, ps) => GetPersonInfo(g, ps));
    
        //其等价的LINQ语句为:
        //var results4 = from p in personList
        //               group p by p.Gender into pGroup
        //               select GetPersonInfo(pGroup.Key, pGroup);
    
        foreach (var result in results4)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine();
    
    
        Console.WriteLine("********第五种用法*************************************");
        //public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer);
        //官方释义:根据键选择器函数对序列中的元素进行分组。通过使用比较器对键进行比较,并且通过使用指定的函数对每个组的元素进行投影。
    
        var groups5 = personList.GroupBy(p => p.Gender, p => new { p.Age, p.Name }, new PersonEqualityComparer());
        foreach (var group in groups5)
        {
            Console.WriteLine(group.Key.ToString());
            foreach (var o in group)
            {
                Console.WriteLine($"	{o.Name},{o.Age}");
            }
        }
    
        Console.WriteLine();
    
        Console.WriteLine("********第六种用法*************************************");
        //public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值。通过使用指定的比较器对键进行比较。
    
        var results6 = personList.GroupBy(p => p.Gender, (p, ps) => GetPersonInfo(p, ps), new PersonEqualityComparer());
        foreach (var result in results6)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine();
    
        Console.WriteLine("********第七种用法*************************************");
        //public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector);
        //官方释义:根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值。通过使用指定的函数对每个组的元素进行投影。
        string GetPersonInfo_names(string gender, IEnumerable<string> names)
        {
            string result = $"{gender}:	";
            foreach (var name in names)
            {
                result += $"{name}	";
            }
            return result;
        }
        var results7 = personList.GroupBy(p => p.Gender, p => p.Name, (g, ns) => GetPersonInfo_names(g, ns));
        foreach (var result in results7)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine("********第八种用法*************************************");
    
        //第八种用法:
        //public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer);
        //官方释义: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值。通过使用指定的比较器对键值进行比较,并且通过使用指定的函数对每个组的元素进行投影。
        var results = personList.GroupBy(p => p.Gender, (p => new { p.Age, p.Name }), (g, ns) =>
                                             {
                                                 string result = $"{g.ToString()}:	";
                                                 foreach (var n in ns)
                                                 {
                                                     result += $"	{n.Name},{n.Age}";
                                                 }
                                                 return result;
                                             }, new PersonEqualityComparer());
        foreach (var result in results)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine();
    
    }
    
    
    class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
        public string Gender { set; get; }
        public override string ToString() => Name;
    }
    
    class PersonEqualityComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y) => x.ToUpper() == y.ToUpper();
        public int GetHashCode(string obj) => obj.ToUpper().GetHashCode();
    
    }
    结果:
    ********第一种用法*************************************
    Male
      P1,18
    male
      P2,19
    Female
      p2,17
      p3,17
    ********第二种用法*************************************
    Male
      P1,18
      P2,19
    Female
      p2,17
      p3,17
    
    ********第三种用法*************************************
    Male
      P1,18
    male
      P2,19
    Female
      p2,17
      p3,17
    
    ********第四种用法*************************************
    Male:  P1,18  
    male:  P2,19  
    Female:  p2,17  p3,17  
    
    ********第五种用法*************************************
    Male
      P1,18
      P2,19
    Female
      p2,17
      p3,17
    
    ********第六种用法*************************************
    Male:  P1,18  P2,19  
    Female:  p2,17  p3,17  
    
    ********第七种用法*************************************
    Male:  P1  
    male:  P2  
    Female:  p2  p3  
    ********第八种用法*************************************
    Male:    P1,18  P2,19
    Female:    p2,17  p3,17
  • 相关阅读:
    波段是金牢记六大诀窍
    zk kafka mariadb scala flink integration
    Oracle 体系结构详解
    图解 Database Buffer Cache 内部原理(二)
    SQL Server 字符集介绍及修改方法演示
    SQL Server 2012 备份与还原详解
    SQL Server 2012 查询数据库中所有表的名称和行数
    SQL Server 2012 查询数据库中表格主键信息
    SQL Server 2012 查询数据库中所有表的索引信息
    图解 Database Buffer Cache 内部原理(一)
  • 原文地址:https://www.cnblogs.com/springsnow/p/14994692.html
Copyright © 2020-2023  润新知