【问题标题】:GroupBy with CountGroupBy 与计数
【发布时间】:2015-04-11 14:41:57
【问题描述】:

我返回了列表 testList,其中包含 5 个具有正确值的项目。

var testList = _context.LectureReportStudent
    .Include(x => x.LectureReport)
    .Include(x => x.LectureReport.Lecture)
    .Include(x => x.Student)
    .Where(x => stuIds.Contains(x.LectureReport.LectureId))
    .Select(x => new StudentModel
    {
        Name = x.Student.FirstName + "" + x.Student.LastName,
        Position = x.Student.JobTitle,
        JobId = x.LectureReport.LectureId,
        StudentId = x.StudentlId,
        LectureId = x.LectureReportId,
        Date = x.Date
    };

我进一步GroupBy这个名单上StudentId

var result = testList.GroupBy(x => x.StudentId)

每个项目都有以下型号

Date, DaysOnLecture, LectureCount, LectureId, LectureReportId, Name, StudentId, Position

我正在尝试获取 Count of DaysOnLecture、LectureCount 属性。

LectureCount 表示没有。学生参加讲座的次数(在我的模型中定义)。

DatsOnLecture 学生参加本次讲座的天数(在我的模型中定义)。

我对逻辑感到震惊。有什么想法吗?

【问题讨论】:

  • 在投影中您没有选择要计算的属性
  • 我没有添加它们,因为它们是计算值。例如,如果我添加它们,我应该为它们分配什么值?
  • 当你说“每个项目都有以下型号...”时,项目是什么?
  • LectureIdLectureReportId 不同吗?即,这是主外键关系吗?
  • 是的,肖恩,它是主外键

标签: c# linq entity-framework linq-to-entities


【解决方案1】:
var counts = testList.GroupBy(x => x.StudentId)
                     .Select(g =>
    new
    {
        StudentId = g.Key,
        TotalDays = g.Sum(gg => gg.DaysOnLecture),
        TotalLecture = g.Sum(gg => gg.LectureCount)
    });

如果您想要每个 LectureId 的总数,那么您必须按 LectureId 对每个学生的数据进行进一步分组

【讨论】:

    【解决方案2】:

    注意:目前,这更多是为了澄清您的问题,而不是实际答案。

    我已经开始使用 Fiddle 来尝试澄清您要完成的工作:https://dotnetfiddle.net/SB02lQ 这不是答案;这是为了澄清您要完成的工作。

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public static class Program
    {
        public static void Main()
        {
            var LectureReportStudent = GetLectureReportStudent();
    
            var testList = LectureReportStudent
    
            .Select(x => new StudentModel {
    
                Name = x.Student.FirstName + " " + x.Student.LastName,
                Position = x.Student.JobTitle,
                JobId = x.LectureReport.LectureId,
                StudentId = x.StudentlId,
                LectureId = x.LectureReportId,
                Date = x.Date
    
            }).GroupBy(x => new {
    
                StudentId = x.StudentId,
                LectureId = x.LectureId
    
            }).Select(x => new StudentModel() {
    
                StudentId = x.Key.StudentId, 
                LectureId = x.Key.LectureId,
                DaysOnLecture = x.Count(),
                LectureCount = -1 // how do we sum all the DaysOnLecture??
    
            }).ToList();
    
            foreach(var t in testList)
            {
                Console.WriteLine("StudentId: " + t.StudentId);
                Console.WriteLine("LectureId: " + t.LectureId);
                Console.WriteLine("DaysOnLecture: " + t.DaysOnLecture);
                Console.WriteLine("LectureCount: " + t.LectureCount);
    
                Console.WriteLine();
            }
        }
    
        public static void Dump(this IEnumerable<StudentModel> query, 
            string title)
        {
            Console.WriteLine(title);
    
            foreach(var i in query)
            {
                Console.WriteLine();
    
                Console.Write(i.Name + ", ");           
                Console.Write(i.Position + ", ");
                Console.Write(i.JobId + ", ");
                Console.Write(i.StudentId + ", ");
                Console.Write(i.LectureId + ", ");
                Console.Write(i.Date + "");
            }
    
            Console.WriteLine();
        }
    
        public static List<LectureReportStudent> GetLectureReportStudent()
        {
            var list = new List<LectureReportStudent>();
    
            var lectureId = 0;
            var studentId = 0;
            for(var i = 0; i < 24; ++i)
            {
                if(i % 12 == 0)
                {
                    ++studentId;
                    lectureId = 1;
                }
    
                if(i % 3 == 0)
                    ++lectureId;
    
                var lrs = new LectureReportStudent()
                {
                    LectureReport = new LectureReport() { 
                        LectureId = lectureId 
                    },
                    Student = new Student() { 
                        FirstName = "foo", 
                        LastName = "bar", 
                        JobTitle = "Job" },
                    StudentlId = studentId,
                    LectureReportId = lectureId,
                    Date = DateTime.Now
                };
                list.Add(lrs);
    
                // Console.WriteLine(studentId + ":" + lectureId);
            }
    
            return list;
        }   
    
        public class LectureReportStudent
        {
            public LectureReport LectureReport { get; set; }
            public Student Student { get; set; }
            public int StudentlId { get; set; }
            public int LectureReportId { get; set; }
            public DateTime Date { get; set; }
        }
    
        public class LectureReport
        {
            public int LectureId { get; set; }
            public Lecture Lecture { get; set; }
        }
    
        public class Lecture {}
    
        public class Student 
        {
            public int stuIds { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string JobTitle { get; set; }        
        }
    
        public class StudentModel 
        {
            public string Name  { get; set; }
            public string Position { get; set; }
            public int JobId { get; set; }
            public int StudentId { get; set; }
            public int LectureId { get; set; }
            public DateTime Date  { get; set; }
            public int DaysOnLecture  { get; set; }
            public int LectureCount  { get; set; }
            public int LectureReportId  { get; set; }
        }
    }
    

    虽然这不是一个答案,但它可能会帮助您找到答案。

    【讨论】:

    • 谢谢,我想我正在寻求解决方案。在您的示例结果中给出了三个 IEnumerable 列表...在第一个列表下,我有 2 条记录(项目),其中包含 LectureId 和 LectureReportId。在第二个列表下,我再次有 2 条记录(项目),其中包含 LectureId 和 LectureReportId。在第三个列表下,我有 1 条记录(项目),带有 ReportId 和 LectureReportId。我的要求是计算 LectureId 和 LectureReportId (Distinct)。请帮帮我。
    • 当然。我已根据您的评论更新了答案。
    • 如果是主外键关系,LectureId 的计数和LectureReportId 的计数有什么区别?
    • Shaun,感谢所有这一切,但我仍然没有抓住重点。我发布了另一个问题,其中包含我正在使用的正确数据。请看一看。 stackoverflow.com/questions/29586572/…
    • @user1874957 不用担心。我的答案还不是答案(这实际上可能是对 SO 论坛的滥用。)相反,它是试图更深入地理解您的问题。探索很有趣,听起来你找到了答案。这是好事。似乎我们能够在这里澄清问题,并且您是其他问题导致解决方案。我还问了另一个类似的问题:stackoverflow.com/questions/29583931/…
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2018-03-21
    相关资源
    最近更新 更多