简体   繁体   English

如何在LINQ Asp C#中使用分组方式

[英]How to use group by in LINQ Asp c#

Reverse Engineering some reports that contain bad data. 对某些包含不良数据的报告进行反向工程。 Reports written in Asp.Net C# using Linq 使用Linq用Asp.Net C#编写的报告

I can write the query in the DB, but having trouble translating to Linq. 我可以在数据库中编写查询,但无法转换为Linq。

public StudentBehaviourActivityDTO GetStudentBehaviourActivity(
            Guid userId)
        {
            List<DateTime> lastUpdatedDates = Uow.ActivityTrackingUnitValues
                .Where(atuv => atuv.Last_Updated_On != null
                    && atuv.TBH_Activity_Tracking_Unit
                        .StudentBehaviour
                        .Student
                        .TeamMembers
                        .Any(tm => tm.Id == userId))
                        .Select(atuv => (DateTime)DbFunctions
                            .TruncateTime(atuv.Last_Updated_On))
                        .ToList();

What is this loading in the toList? toList中的此加载是什么? I assume this is loading my table: ActivityTrackingUnitValues 我假设这正在加载我的表:ActivityTrackingUnitValues

But then it also does then it also looks like it's joining these tables 但是然后它也确实这样做了,好像它正在联接这些表

.StudentBehaviour
.Student
.TeamMembers

I want to take the max(value) for Last_Updated_On and then group by student 我想取Last_Updated_On的max(value),然后按学生分组

Any help or explanation is greatly appreciated. 任何帮助或解释,我们将不胜感激。

public StudentBehaviourActivityDTO GetStudentBehaviourActivity(
        Guid userId)
    {
        List<DateTime> lastUpdatedDates = Uow.ActivityTrackingUnitValues
            .Where(atuv => atuv.Last_Updated_On != null // check to make sure it's not null
                && atuv.TBH_Activity_Tracking_Unit
                    .StudentBehaviour //this references a class (generally a join, but it doesn't join!)
                    .Student //this references a class (generally a join, but it doesn't join!)
                    .TeamMembers //this references a class (generally a join, but it doesn't join!)
                    .Any(tm => tm.Id == userId)) // Checks to make sure it isn't null or has a count greater than 0
                    .Select(atuv => (DateTime)DbFunctions
                        .TruncateTime(atuv.Last_Updated_On)) // Returns the truncatedtime and nowthing else
                    .ToList(); // converts it to a list

From what I can gather, it's making sure the class has members available. 据我所知,它确保班上有可用的成员。

What is this loading in the toList? toList中的此加载是什么?

Basically it seems it loads the last updated dates of the subjected user if he is in a team and updated ... 基本上,如果受训用户在团队中并进行了更新,似乎可以加载受训用户的最后更新日期。

.StudentBehaviour
.Student
.TeamMembers

Those are navigational properties mainly translated into joins to get the desired data with relation to the table in hand. 这些是导航属性,主要转换为联接以获取与手中表有关的所需数据。

so in details it does the following: 因此,它详细执行以下操作:

  1. Gets the list of Activity Tracking Unit Values where: 获取活动跟踪单元值的列表,其中:
    • The activity tracking unit value is updated 活动跟踪单位值已更新
    • The student we are checking is a team member with the student whose behaviour is tracked by the unit responsible for this activity tracking unit value 我们正在检查的学生是该学生的团队成员,其行为由负责此活动跟踪单位值的单位跟踪
  2. Selects the day where those values are last updated 选择上次更新这些值的日期

I want to take the max(value) for Last_Updated_On and then group by student 我想取Last_Updated_On的max(value),然后按学生分组

compose the base dataset: 组成基本数据集:

.Select(atuv => new 
{ 
    LastUpdatedDate = (DateTime)DbFunctions.TruncateTime(atuv.Last_Updated_On)),
    StudentId = atuv.TBH_Activity_Tracking_Unit.StudentBehaviour.Student.Id
})

group by student id: 按学生编号分组:

.GroupBy(sd => sd.StudentId)

select student and it max last updated date: 选择学生,并且它是最大上次更新日期:

.Select(sd => new { StudentId = sd.Key, MaxDate = sd.Max(d => d.LastUpdatedDate) });

finally it will look something like this: 最终它将看起来像这样:

var maxUpdatedDatePerStudentList = Uow.ActivityTrackingUnitValues
                    .Where(atuv => atuv.Last_Updated_On != null
                                && atuv.TBH_Activity_Tracking_Unit
                                       .StudentBehaviour
                                       .Student
                                       .TeamMembers
                                       .Any(tm => tm.Id == userId))
                    .Select(atuv => new 
                    { 
                        LastUpdatedDate = (DateTime)DbFunctions.TruncateTime(atuv.Last_Updated_On)),
                        StudentId = atuv.TBH_Activity_Tracking_Unit.StudentBehaviour.Student.Id
                    })
                    .GroupBy(sd => sd.StudentId)
                    .Select(sd => new { StudentId = sd.Key, MaxDate = sd.Max(d => d.LastUpdatedDate) })
                    .ToList();

and you can access its data like this: 您可以像这样访问其数据:

maxUpdatedDatePerStudentList.ForEach(g => Console.WriteLine($"Student {g.StudentId} max updated date is {g.MaxDate}"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM