C# LINQ直接连接到对象

C# LINQ直接连接到对象,c#,linq,C#,Linq,我对LINQ比较陌生,我已经实现了一个LINQ语句,它进行连接,然后我得到查询结果,迭代结果,并将每个结果分配给一个新对象,然后将该对象添加到对象列表中。有没有一种更优雅的方法来执行以下操作,这样我就可以直接选择对象列表了 谢谢,非常感谢 var clubAttendeeEducationList = new List<ClubAttendeeEducation>(); var r = (from oer in db.OnlineEducatio

我对LINQ比较陌生,我已经实现了一个LINQ语句,它进行连接,然后我得到查询结果,迭代结果,并将每个结果分配给一个新对象,然后将该对象添加到对象列表中。有没有一种更优雅的方法来执行以下操作,这样我就可以直接选择对象列表了

谢谢,非常感谢

        var clubAttendeeEducationList = new List<ClubAttendeeEducation>();



        var r = (from oer in db.OnlineEducationRegistrations
                join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
                    oec.OnlineEducationCourseId
                where
                    (oer.MasterCustomerId.Equals(userId) && oer.DateCompleted >= start.Date &&
                     oer.DateCompleted <= upUntil.Date && oer.DateCompleted != null)

                select new {OnlineEducationRegistration = oer, oec.CourseTitle}).ToList();

        foreach (var item in r)
        {

            var educationItem = new ClubAttendeeEducation
                    {
                        Session = item.CourseTitle,
                        Date = item.OnlineEducationRegistration.DateCompleted.Value

                    };

            clubAttendeeEducationList.Add(educationItem);
        }


    return clubAttendeeEducationList;
var clubattendeducationlist=新列表();
var r=(来自db.Online EducationRegistrations中的oer)
在db.OnlineEducationCourseId equals上加入oec在线教育课程
在线教育课程
哪里
(oer.MasterCustomerId.Equals(userId)&&oer.DateCompleted>=开始日期&&

oer.DateCompleted正如您在查询中创建新的匿名对象一样,您可以创建任何类型的对象,并将结果转换为列表

var clubAttendeeEducationList = (from oer in db.OnlineEducationRegistrations
                                /* rest of the query */
                                select new ClubAttendeeEducation
                                {
                                    Session = item.CourseTitle,
                                    Date = item.OnlineEducationRegistration.DateCompleted.Value
                                }).ToList();

如果您确实需要其他类型的对象,请不要选择匿名对象。只需直接在原始查询中选择您实际需要的类型。

那么,您为什么要使用
select new{…}
而不是
select new clubattendeeducation{Session=oec.CourseTitle,Date=oer.DateCompleted.Value}
?不清楚您为什么选择这种方法…@JonSkeet我的想法是,我不需要连接表中的所有列,所以我这样做的原因是:选择new{OnlineEducationRegistration=oer,oec.CourseTile})。ToList();就像我说的,我对LINQ有点陌生,所以我需要一些指导。