2011-01-25 3 views
1

나는 개체의 목록을 가지고, 그 객체 또는 연락처 정보가 없을 수 있습니다LINQ 가입 및 GroupJoin

  // Join contact 
     query = query.Join(
     (new ContactRepository(this.Db)).List().Where(x => x.IsMainContact), 
     x => new { x.ListItem.ContentObject.LoginId }, 
     y => new { y.LoginId }, 
     (x, y) => new ListItemExtended<ListItemFirm> 
     { 
      City = y.City, 
      State = y.State, 
      Country = y.Country 
     }); 

이 내면의 수행은 '은 loginid'에 가입 할 수 있습니다. 하지만 연락처 정보가 주어진 LoginId에 존재하지 않으면 비어있을 수 있도록 outter join이 필요합니다. 당신은 외부 수동으로 조인 실행해야

감사

답변

1

도와주세요 :

  var contacts = (new ContactRepository(this.Db)).List(); 
      query.Select(item => 
      { 
       var foundContact = contacts.FirstOrDefault(contact => contact.Id == item.Id); 
       return new ListItemExtended<ListItemFirm>() 
        { 
         Id = item.Id, 
         City = foundContact != null ? foundContact.City : null, 
         State = foundContact != null ? foundContact.State : null, 
         Country = foundContact != null ? foundContact.Country : null, 
        }; 
      }) 

을하지만 당신의 연락처 항목이 구조체의 경우 기억 - 널 (null)를 확인하는 것은 적절한 방법이 아니다. FirstOrDefault() 대신 Any() 연산자를 사용하십시오.