By Radhi

2011-02-18 06:24:43 8 Comments

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


@Adam Rackis 2011-02-18 06:43:53

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

@Radhi 2011-02-18 06:45:39

here peopletype may be multiple for one people...

@Radhi 2011-02-18 06:46:25

and i want final result as collection of people which contains list of peopletype associated with it.... so i think my situation is different then the query you wrote... or else let me know if i am missing anything

@Adam Rackis 2011-02-18 06:47:24

Why do you say that? I would think Person has a PersonType property, which matches up with one and only one PersonType.TypeID. If this were a database schema PersonType.TypeID would be the PK. Am I missing something?

@Adam Rackis 2011-02-18 06:49:20

@Radhi I'm afraid I don't follow. I was just trying to give you a very general join form for Lambdas. You're going to give a lot more detail in your question. Having said that, the short answer is you can apply whatever where you want to filter this down.

@Radhi 2011-02-18 06:57:16

i have updated the post with sample code

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Retrieving Property name from lambda expression

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

14 Answered Questions

[SOLVED] list comprehension vs. lambda + filter

9 Answered Questions

[SOLVED] What is a lambda expression in C++11?

9 Answered Questions

[SOLVED] Why would you use Expression<Func<T>> rather than Func<T>?

11 Answered Questions

9 Answered Questions

[SOLVED] Join/Where with LINQ and Lambda

  • 2010-05-04 17:53:38
  • David
  • 793864 View
  • 408 Score
  • 9 Answer
  • Tags:   c# linq join lambda

18 Answered Questions

[SOLVED] Distinct() with lambda?

26 Answered Questions

[SOLVED] Why are Python lambdas useful?

7 Answered Questions

[SOLVED] Entity Framework 5 Updating a Record

Sponsored Content