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

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

@陈卧龙的博客

相关文章
|
4月前
|
存储 安全 编译器
C# 11.0中的泛型属性:类型安全的新篇章
【1月更文挑战第23天】C# 11.0引入了泛型属性的概念,这一新特性为开发者提供了更高级别的类型安全性和灵活性。本文将详细探讨C# 11.0中泛型属性的工作原理、使用场景以及它们对现有编程模式的改进。通过深入了解泛型属性,开发者将能够编写更加健壮、可维护的代码,并充分利用C#语言的最新发展。
|
4月前
|
SQL 开发框架 .NET
C#进阶-LINQ实现对集合的增删改查
本篇演示了LINQ在日常开发中的常用操作,实现结果集的增删改查。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
40 0
|
2月前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)
|
1天前
|
SQL 开发框架 .NET
C#linq表达式的应用
C#linq表达式的应用
4 0
|
2月前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---group和join相关用法(三)
C#学习相关系列之Linq用法---group和join相关用法(三)
|
2月前
|
开发框架 .NET C#
C#学习相关系列之Linq常用方法---排序(一)
C#学习相关系列之Linq常用方法---排序(一)
|
2月前
|
存储 安全 Java
34.C#:listT泛型集合
34.C#:listT泛型集合
18 1
|
2月前
|
开发框架 安全 .NET
C# .NET面试系列三:集合、异常、泛型、LINQ、委托、EF!
<h2>集合、异常、泛型、LINQ、委托、EF! #### 1. IList 接口与 List 的区别是什么? IList 接口和 List 类是C#中集合的两个相关但不同的概念。下面是它们的主要区别: <b>IList 接口</b> IList 接口是C#中定义的一个泛型接口,位于 System.Collections 命名空间。它派生自 ICollection 接口,定义了一个可以通过索引访问的有序集合。 ```c# IList 接口包含一系列索引化的属性和方法,允许按索引访问、插入、移除元素等。 由于是接口,它只定义了成员的契约,而不提供具体的实现。类似于 IEnumera
166 2
|
3月前
|
SQL 开发框架 .NET
C# Linq SaveChanges()报错 You have an error in your SQL syntex
C# Linq SaveChanges()报错 You have an error in your SQL syntex
11 0
|
4月前
|
存储 安全 算法
C# 泛型:类型参数化的强大工具
【1月更文挑战第7天】本文将深入探讨C#语言中的泛型编程,包括泛型的定义、用途、优势以及实际应用。通过类型参数化,泛型允许开发者编写更加灵活且可重用的代码,同时提高程序的类型安全性和性能。本文将通过示例代码和详细解释,帮助读者更好地理解泛型在C#中的重要性和实用性。