Lambda Expression for join

后端 未结 1 907
自闭症患者
自闭症患者 2020-12-02 23:41
public class CourseDetail
    {
        public CourseDetail();
        public string CourseId { get; set; }
        public string CourseDescription { get; set; }
            


        
相关标签:
1条回答
  • 2020-12-03 00:22

    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 };
    
    0 讨论(0)
提交回复
热议问题