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($"\t{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($"\t{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($"\t{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}:\t";
        foreach (var p in persons)
        {
            result += $"{p.Name},{p.Age}\t";
        }
        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($"\t{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}:\t";
        foreach (var name in names)
        {
            result += $"{name}\t";
        }
        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()}:\t";
                                             foreach (var n in ns)
                                             {
                                                 result += $"\t{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

posted on 2021-07-10 19:52  springsnow  阅读(273)  评论(0编辑  收藏  举报

导航