用于连接的Lambda表达式
public class CourseDetail { public CourseDetail(); public string CourseId { get; set; } public string CourseDescription { get; set; } public long CourseSer { get; set; } } public class RefUIDByCourse { public long CourseSer { get; set; } public double DeliveredDose{ get; set; } public double PlannedDose{ get; set; } public string RefUID { get; set; } } public class RefData { public double DailyDoseLimit { get; set; } public string RefName { get; set; } public string RefUID { get; set; } public double SessionDoseLimit { get; set; } } public class CourseSummary { public long CourseSer { get; set; } public double DeliveredDose{ get; set; } public double PlannedDose{ get; set; } Public List lstRefData {get;set;} }
对于一个courseSer,RefUIDByCourse中可以有多个RefUID,对于每个RefUID,RefData中都会有一个记录
我有CourseDetail,RefUIDByCourse和RefData的列表,现在课程中存在课程,我必须创建CourseSummary列表。
我可以做的一件事是为coursedetail做循环并使用linq查询获取相应的refdata并创建一个courseummary对象并将其添加到列表中。
但有没有办法通过一个linq查询来做,而不是做循环
Join的lambda有点涉及 – 这是一个简单的例子:
List People = new List (); List PeopleTypes = new List (); var joined = People.Join(PeopleTypes, PeopleKey => PeopleKey.PersonType, PeopleTypesKey => PeopleTypesKey.TypeID, (Person, PersoneType) => new { Name = Person.Name, TypeID = PersoneType.TypeID });
我通常发现查询语法比lambdas更易于加入
var joined2 = from p in People join pType in PeopleTypes on p.PersonType equals pType.TypeID where p.Name.StartsWith("whatever") select new { Name = p.Name, TypeID = pType.TypeID };