C# 泛型分组和Linq分组的异同-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

简介: 没什么好说的,因为用的到,所以作个记录, 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.

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

代码如下:

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; }
    }
}

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

@陈卧龙的博客

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章