首页 > 解决方案 > 如何在 LINQ Asp c# 中使用 group by

问题描述

对一些包含不良数据的报告进行逆向工程。使用 Linq 用 Asp.Net C# 编写的报告

我可以在数据库中编写查询,但无法转换为 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();

toList 中的这个加载是什么?我假设这是加载我的表:ActivityTrackingUnitValues

但是它也确实如此,看起来它也正在加入这些表

.StudentBehaviour
.Student
.TeamMembers

我想取 Last_Updated_On 的最大值(值),然后按学生分组

非常感谢任何帮助或解释。

标签: c#linq

解决方案


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

据我所知,它确保班级有可用的成员。


推荐阅读