c# linq goup by实例

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> Students = GetStudents();
            //group by 一个字段
            var query1 = from a in Students
                         group a by a.SchoolID into b
                         select new
                         {
                             SchoolID = b.Key,
                             Count = b.Count()
                         };
            //group by 多个字段
            var query2 = from a in Students
                         group a by new { SchoolID = a.SchoolID, SchoolName = a.SchoolName } into b
                         select new
                         {
                             SchoolID = b.Key.SchoolID,
                             SchoolName = b.Key.SchoolName,
                             Count = b.Count()
                         };
            //group by 能获取子集合
            var query3 = Students.GroupBy(a => new { SchoolID = a.SchoolID, SchoolName = a.SchoolName });
            if (query3.Count() > 0)
            {
                foreach (var item in query3)
                {
                    int SchoolID = item.Key.SchoolID;
                    string SchoolName = item.Key.SchoolName;
                    List<Student> list = item.ToList();
                }
            }
        }
        private static List<Student> GetStudents()
        {
            List<Student> students = new List<Student>();
            students.Add(new Student(1, "student1", 1, "学校1"));
            students.Add(new Student(2, "student2", 1, "学校1"));
            students.Add(new Student(3, "student3", 2, "学校2"));
            students.Add(new Student(4, "student4", 2, "学校2"));
            students.Add(new Student(5, "student5", 3, "学校3"));
            return students;
        }
    }
    public class Student
    {
        public Student() { }
        public Student(int _studentID, string _studentName, int _schoolID, string _schoolName)
        {
            this.StudentID = _studentID;
            this.StudentName = _studentName;
            this.SchoolID = _schoolID;
            this.SchoolName = _schoolName;
        }
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int SchoolID { get; set; }
        public string SchoolName { get; set; }
    }
}

group by多个对象

var test = from l in list
            join j in existsJobList on l.JobName equals j.Code
            join d in existsDeptList on l.DeptCode equals d.Code
            group new { l, j, d }
            by new { PositionName = l.PositionName, JobTitleId = j.JobTitleId, DeptId = d.DeptId } into b
            select new
            {
                PositionName = b.Key.PositionName,
                JobTitleId = b.Key.JobTitleId,
                DeptId = b.Key.DeptId,
            };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值