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<RefData> lstRefData {get;set;}
}
对于一门类(class),RefUIDByCourse中可以有多个RefUID
对于每个RefUID,RefData中都会有一个记录
我有CourseDetail,RefUIDByCourse和RefData的列表
现在,对于CourseerDetail中存在的Courser,我必须创建CourseSummary的列表。
我可以做的一件事是为coursedetail进行循环,并使用linq查询获取相应的refdata,并创建一个coursesmary的对象并将其添加到列表中。
,但是有什么方法可以通过一个linq查询来完成,而不是通过进行循环
最佳答案
Join的lambda有点涉及-这是一个简单的示例:
List<Person> People = new List<Person>();
List<PersonType> PeopleTypes = new List<PersonType>();
var joined = People.Join(PeopleTypes,
PeopleKey => PeopleKey.PersonType,
PeopleTypesKey => PeopleTypesKey.TypeID,
(Person, PersoneType) => new
{
Name = Person.Name,
TypeID = PersoneType.TypeID
});
我通常发现查询语法比lambda更易读
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 };