Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - Lambda Expression for join

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

For one courseSer there can be multiple RefUID in RefUIDByCourse and for every RefUID there will be one record in RefData

i have list of CourseDetail,RefUIDByCourse and RefData now for the courseser exist in coursedetail i have to create list of CourseSummary.

one thing i can do is do for loop for coursedetail and fetch respective refdata using linq query and create a object of coursesummary and add it in list.

but is there any way to do it by one linq query instead of doing loop through

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lambda for a Join is a bit involved - here's a simple example:

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

I usually find the query syntax a lot more readable than lambdas for joining

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...