将项目分组为项目和子项目

时间:2015-07-02 12:12:28

标签: c# linq entity-framework group-by

我正在使用EF并且我的查询返回以下匿名对象的集合

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

我有两个以下的课程

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

结果集可能有多个课程,每个学生都注册了

如何将结果集转换为如图所示的对象列表。

问候。

3 个答案:

答案 0 :(得分:2)

我找到了解决方案。以下查询可以解决问题:

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();

答案 1 :(得分:0)

我想知道为什么EF查询会带回那样的非规范化数据,如果你不能解决那里的问题,但有时你必须使用你所拥有的。

这应该可以解决问题:

// 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(),                                                                                 
    });

答案 2 :(得分:0)

以下对我来说非常有效

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();