简体   繁体   English

将项目分为项目和子项目

[英]Group items into an item and sub items

I am using EF and and my query is returning a collection of the following as anonymous object 我正在使用EF,并且我的查询返回以下集合作为匿名对象

new
{
    CourseId,
    CourseCode,
    StudentId,
    StudentNo,
    StudentName,
    StudentSurname
}

and I have two following classes 我有两个以下课程

public class Student
{
    public int StudentId { get; set; }
    public string StudentNo { get; set; }
    public string StudentName { get; set; }
    public string StudentSurname { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseCode { get; set; }

    public List<Student> EnrolledStudents { get; set; }
}

The resulset might have several courses with students enrolled in each 结果可能有几门课程,每门课程都有学生报名

How can I convert the result set into an list of objects as shown. 如何将结果集转换为如图所示的对象列表。

Regards. 问候。

I have found the solution. 我找到了解决方案。 The following query does the trick: 以下查询可以解决问题:

List<Course> courses = (from item in resultset
                       group new
                       {
                           item.CourseId,
                           item.CourseCode,
                           item.StudentId,
                           item.StudentNo,
                           item.StudentName,
                           item.StudentSurname
                       }
                       by new
                       {
                           CourseId = item.CourseId,
                           CourseCode = item.CourseCode
                       } into _item
                       select new Course
                       {
                           CourseId = _item.Key.CourseId,
                           CourseCode = _item.Key.CourseCode
                           EnrolledStudents  = (from st in resultset.Where(x => x.CourseId == _item.Key.CourseId)
                                               select new Student
                                               {
                                                   StudentId = st.StudentId,
                                                   StudentNo = st.StudentNo,
                                                   StudentName =  st.StudentName,
                                                   StudentSurname = st.StudentSurname
                                               }).ToList()
                       }).ToList();

I would wonder why the EF query is bringing back denormalized data like that and if you cannot tackle the problem there, but sometimes you have to work with what you've got. 我想知道为什么EF查询会带回这样的非规范化数据,如果您不能在那里解决问题,但是有时您必须使用已有的东西。

This should do the trick: 这应该可以解决问题:

// assume the anon objects are in collection called 'items'
// in memory and data is all valid

// Get just one copy of each student
var students = items
    .Select(i => new { i.StudentId, i.StudentNo, i.StudentName, i.StudentSurname })
    .Distinct()
    .Select(i => new Student { 
        StudentId = i.StudentId, 
        StudentNo = i.StudentNo, 
        StudentName = i.StudentName, 
        StudentSurname = i.StudentSurname
    }); 

// Create courses and match up the students
var courses = items.GroupBy(i => i.CourseId).Select(gc => new Course            
    {   
        CourseId = gc.First().CourseId,
        CourseCode = gc.First().CourseCode,
        EnrolledStudents = gc.Join(students, i => i.StudentId, s => s.StudentId, (i, s) => s).ToList(),                                                                                 
    });

The following does work perfectly for me 以下对我来说确实很不错

var courses = resultset.GroupBy(
    row => new { row.CourseId, row.CourseCode },
    row => new Student
            {
                StudentId = row.StudentId,
                StudentName = row.StudentName,
                StudentNo = row.StudentNo,
                StudentSurname = row.StudentSurname,
            },
    (key, valueList) => new Course
                {
                    CourseId = key.CourseId,
                    CourseCode = key.CourseCode,
                    EnrolledStudents = valueList.ToList()
                }
).ToList();

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

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