GROUPBY以计数 [英] GroupBy with Count

查看:135
本文介绍了GROUPBY以计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表 testList 返回,它拥有的 5 在它适当值的项目。

  VAR testList = _context.LectureReportStudent 
.INCLUDE(X => x.LectureReport)
.INCLUDE(X => x.LectureReport.Lecture )
.INCLUDE(X => x.Student)
。凡(X => stuIds.Contains(x.LectureReport.LectureId))
。选择(X =>新建StudentModel
{
名称= x.Student.FirstName ++ x.Student.LastName,
位置= x.Student.JobTitle,
的JobId = x.LectureReport.LectureId ,
StudentId = x.StudentlId,
LectureId = x.LectureReportId,
日期= x.Date
};

我进一步 GROUPBY 这个名单 StudentId 。<
<; / p>

  / pre> 

每个项目都有下面的模型

 日期,DaysOnLecture ,LectureCount,LectureId,LectureReportId,名称,StudentId,位置

我试图采取DaysOnLecture伯爵,LectureCount属性。




LectureCount表示没有。次学生听课(这是在我的模型中定义)。



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




我触动了逻辑。任何想法


解决方案

注意:就目前而言,这更是澄清你的问题更多的是尝试。实际的答案



我已经开始了一个小提琴试图澄清你想完成什么:的 https://dotnetfiddle.net/SB02lQ 这是不是一个答案; 。这是澄清你想实现什么企图

 使用系统; 
使用System.Linq的;
使用System.Collections.Generic;

公共静态类节目
{
公共静态无效的主要()
{
VAR LectureReportStudent = GetLectureReportStudent();

VAR testList = LectureReportStudent

。选择(X =>新建StudentModel {

名称= x.Student.FirstName ++ X .Student.LastName,
位置= x.Student.JobTitle,
的JobId = x.LectureReport.LectureId,
StudentId = x.StudentlId,
LectureId = x.LectureReportId,
日期= x.Date

})GROUPBY(X =方式>新{

StudentId = x.StudentId,
LectureId = x.LectureId

})选择(X =>新建StudentModel(){

StudentId = x.Key.StudentId,
LectureId = x.Key.LectureId,
DaysOnLecture = x.Count(),
LectureCount = -1 //我们如何总结所有DaysOnLecture?

})了ToList()。

的foreach(在testList变种T)
{
Console.WriteLine(StudentId:+ t.StudentId);
Console.WriteLine(LectureId:+ t.LectureId);
Console.WriteLine(DaysOnLecture:+ t.DaysOnLecture);
Console.WriteLine(LectureCount:+ t.LectureCount);

Console.WriteLine();
}
}

公共静态无效的转储(这IEnumerable的< StudentModel>的查询,
串题)
{
Console.WriteLine(标题);

的foreach(VAR我在查询)
{
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();
}

公共静态列表< LectureReportStudent> GetLectureReportStudent()
{
无功名单=新名单,LT; LectureReportStudent>();

变种lectureId = 0;
变种studentId = 0;
为(VAR I = 0; I< 24; ++ I)
{
如果(I%12 == 0)
{
++ studentId ;
lectureId = 1;
}

如果(I%3 == 0)
++ lectureId;

变种LRS =新LectureReportStudent()
{
LectureReport =新LectureReport(){
LectureId = lectureId
},
学生=新的学生(){
名字=富,
姓氏=酒吧,
JOBTITLE =作业},
StudentlId = studentId,
LectureReportId = lectureId,
日期= DateTime.Now
};
list.Add(LRS);

// Console.WriteLine(studentId +:+ lectureId);
}

返回列表;
}

公共类LectureReportStudent
{
公共LectureReport LectureReport {搞定;组; }
公共学生学生{搞定;组; }
公众诠释StudentlId {搞定;组; }
公众诠释LectureReportId {搞定;组; }
公众的DateTime日期{搞定;组; }
}

公共类LectureReport
{
公众诠释LectureId {搞定;组; }
公益讲座讲座{搞定;组; }
}

公共类讲座{}

公共类学生
{
公众诠释stuIds {搞定;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公共字符串JOBTITLE {搞定;组; }
}

公共类StudentModel
{
公共字符串名称{;组; }
公共字符串位置{搞定;组; }
公众诠释的JobId {搞定;组; }
公众诠释StudentId {搞定;组; }
公众诠释LectureId {搞定;组; }
公众的DateTime日期{搞定;组; }
公众诠释DaysOnLecture {搞定;组; }
公众诠释LectureCount {搞定;组; }
公众诠释LectureReportId {搞定;组; }
}
}



虽然这不是一个答案,它可能帮助你走向一个答案移动。


I have list testList returned and it has 5 items in it with proper values.

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

I further GroupBy this list on StudentId.

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

Each item has the following model

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

I am trying to take the Count of DaysOnLecture, LectureCount attributes.

LectureCount means the no. of times student attended lecture (it is defined in my model).

DatsOnLecture the no of days student attended this lecture (it is defined in my model).

I am struck with logic. Any idea?

解决方案

Note: For now, this is more an attempt to clarify your question than an actual answer.

I've started a Fiddle to try to clarify what you're trying to accomplish: https://dotnetfiddle.net/SB02lQ This isn't an answer; it's an attempt to clarify what you're trying to accomplish.

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

While this isn't an answer, it might help you move toward an answer.

这篇关于GROUPBY以计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆