C# 泛型分组和Linq分组的异同

没什么好说的,因为用的到,所以作个记录,

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleMe
{
    class Program
    {
        static List<Person> persons1 = new List<Person>();
        static void Main(string[] args)
        {
            persons1.Add(new Person("张三", "", 20, 1500));
            persons1.Add(new Person("王成", "", 32, 3200));
            persons1.Add(new Person("李丽", "", 19, 1700));
            persons1.Add(new Person("何英", "", 35, 3600));
            persons1.Add(new Person("何英", "", 18, 1600));

            Console.WriteLine("泛型分组如下:");

            var ls = persons1.GroupBy(a => a.Sex).Select(g => (new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
            foreach (var item in ls)
            {
                Console.WriteLine(item.sex + "  " + item.count + "  " + item.ageC + "   " + item.moneyC);

            }

            ////////////////////////////////////////////////////////////////////////////////////////////////
            Console.WriteLine("");
            Console.WriteLine("LIQN分组如下:");

            var ls2 = from ps in persons1
                      group ps by ps.Sex
                          into g
                          select new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };
            foreach (var item in ls2)
            {
                Console.WriteLine(item.sex + "  " + item.count + "  " + item.ageC + "   " + item.moneyC);

            }

            Console.Read();
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; private set; }
        public string Sex { get; set; }
        public int Money { get; set; }

        public Person(string name, string sex, int age, int money)
        {
            Name = name;
            Age = age;
            Sex = sex;
            Money = money;
        }
    }
}

执行截图:

后续...

敬请期待...

 如果是多列/多个属性参与分组应当如何呢?

代码如下:

namespace Test2
{
    class Program
    {
        static List<Person> persons1 = new List<Person>();
        static void Main(string[] args)
        {
            persons1.Add(new Person("张三", "", 20, 1500, 0));
            persons1.Add(new Person("王成", "", 32, 3200, 0));
            persons1.Add(new Person("李丽", "", 19, 1700, 1));
            persons1.Add(new Person("何英", "", 35, 3600, 1));
            persons1.Add(new Person("王红", "", 18, 1600, 1));

            Console.WriteLine("泛型多属性/多列分组如下:");

            var ls = persons1.GroupBy(A => new { A.Sex, A.SexId }).Select(g => (new { sex = g.Key.Sex, sexId = g.Key.SexId, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
            foreach (var item in ls)
            {
                Console.WriteLine(item.sex + "  " + item.count + "  " + item.ageC + "   " + item.moneyC);

            }

            ////////////////////////////////////////////////////////////////////////////////////////////////
            Console.WriteLine("");
            Console.WriteLine("LIQN多属性/多列分组如下:");

            var ls2 = from ps in persons1
                      group ps by new { ps.Sex,ps.SexId}
                          into g
                          select new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };
            foreach (var item in ls2)
            {
                Console.WriteLine(item.sex + "  " + item.count + "  " + item.ageC + "   " + item.moneyC);

            }

            Console.Read();
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; private set; }
        public string Sex { get; set; }
        public int SexId { get; set; }
        public int Money { get; set; }

        public Person(string name, string sex, int age, int money, int sexId)
        {
            Name = name;
            Age = age;
            Sex = sex;
            Money = money;
            SexId = sexId;
        }
    }

    public class PersonGroup
    {
        public string Sex { get; set; }
        public int SexId { get; set; }
        public List<NewPerson> PersonLs { get; set; }
    }

    public class NewPerson
    {
        public string Name { get; set; }
        public int Age { get; private set; }
        public int Money { get; set; }
    }
}

未完,持续...

敬请期待...

本次补充AutoMapper的使用,我们将分组的数据映射到类,代码如下:

程序集下载地址:http://files.cnblogs.com/files/chenwolong/AutoMapper.rar   

也可通过NuGet获取

需要程序集AutoMapper.dll  引入命名空间:using AutoMapper;

namespace ConsoleMe
{
    class Program
    {
        static List<Person> persons1 = new List<Person>();
        static void Main(string[] args)
        {
            persons1.Add(new Person("张三", "", 20, 1500, 0));
            persons1.Add(new Person("王成", "", 32, 3200, 0));
            persons1.Add(new Person("李丽", "", 19, 1700, 1));
            persons1.Add(new Person("何英", "", 35, 3600, 1));
            persons1.Add(new Person("王红", "", 18, 1600, 1));

            Console.WriteLine("泛型多属性/多列分组如下:");

            var ls = persons1.GroupBy(A => new { A.Sex, A.SexId }).Select(g => (new { sex = g.Key.Sex, sexId = g.Key.SexId, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
            foreach (var item in ls)
            {
                Console.WriteLine(item.sex + "  " + item.count + "  " + item.ageC + "   " + item.moneyC);

            }

            ////////////////////////////////////////////////////////////////////////////////////////////////
            Console.WriteLine("");
            Console.WriteLine("LIQN多属性/多列分组如下:");

            var ls2 = from ps in persons1
                      select new { sex = ps.Sex, Name = ps.Name, Age = ps.Age, Money = ps.Money, SexId = ps.SexId };
            var Kar = ls2.GroupBy(g => new { g.sex, g.SexId }).Select(S => new { S.Key.sex, S.Key.SexId, PersonList = S });
            //使用AutoMap 将 kar 映射到对应的类
            List<PersonGroup> AllList = Mapper.DynamicMap<List<PersonGroup>>(Kar);
            foreach (var item in AllList)
            {
                Console.WriteLine("性别为" + item.Sex + "的童鞋有:");
                int index = AllList.IndexOf(item);
                foreach (var childItem in AllList[index].PersonList)
                {
                    Console.WriteLine("姓名为:" + childItem.Name + ",年龄为:" + childItem.Age + ",金钱为:" + childItem.Money + "");
                }
            }


            var data = Kar.ToList();

            Console.Read();
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; private set; }
        public string Sex { get; set; }
        public int SexId { get; set; }
        public int Money { get; set; }

        public Person(string name, string sex, int age, int money, int sexId)
        {
            Name = name;
            Age = age;
            Sex = sex;
            Money = money;
            SexId = sexId;
        }
    }

    public class PersonGroup
    {
        public string Sex { get; set; }
        public int SexId { get; set; }
        public List<NewPerson> PersonList { get; set; }
    }

    public class NewPerson
    {
        public string Name { get; set; }
        public int Age { get; private set; }
        public int Money { get; set; }
    }
}

以上代码适用于:一对多的类映射

@陈卧龙的博客

原文地址:https://www.cnblogs.com/chenwolong/p/GroupBy.html