2016-11-24 1 views
0

contact_list 테이블과 Many-Many 관계를 갖는 CustomerGroup 테이블이 있습니다. 세 번째 테이블 CustomerGroupContact 두 테이블의 기본 키가 있습니다. 나는 모양을 객체를 생성하기 위해 두 테이블을 조인하기 위해 노력하고있어Linq와 many to many relationship 쿼리

public class Contact_List 
{ 
    [Key] 
    public int Contact_List_Code { get; set; } 

    public int Customer_Code { get; set; } 

    public string First_Name { get; set; } 

    public string Last_Name { get; set; } 

    public string Contact_No { get; set; } 

} 

:

public class CustomerGroup 
{ 
    public CustomerGroup() 
    { 
     CustomerGroupContacts = new HashSet<CustomerGroupContact>(); 

    } 

    [Key] 
    public int Customer_Group_Code { get; set; } 

    public int Customer_Code { get; set; } 

    public string Customer_Group_Name { get; set; } 


    public virtual ICollection<CustomerGroupContact> CustomerGroupContacts { get; set; } 

} 

여기처럼 Contact_List 모델이 어떻게 표시되는지를 보여줍니다 : 여기

는 CustomerGroup 테이블의 모습입니다 아래 모델 :

public class Contacts 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string ContactNo { get; set; } 
    public string GroupName { get; set; } 
} 

올바른 쿼리 문을 사용하는 데 어려움을 겪고 있습니다. customer_code 속성을 기반으로 테이블을 조인합니다. 도움을 주시면 감사하겠습니다.

+1

나는 당신이 ORM을 사용하고, 일부 재산에'[키]'속성이 참조 (같은 엔티티 프레임 워크)? –

+0

http://pastebin.com/gYkTmKyA. 이것은 내가 지금까지 가지고있는 것이지만 너무 노골적으로 잘못되었다는 것을 알고있다. – psyoptica

+0

@RemyGrandin 예 코드 첫 번째 모델에서 EF를 사용하고있다. – psyoptica

답변

-1

희망이 작동합니다 :

var userContactList = (from custGroup in _db.CustomerGroup 
          join  cList in _db.Contact_List 
          on custGroup.Customer_Code equals cList.Customer_Code 
          select new Contacts { 
              FirstName = cList.First_Name, 
              LastName = cList.Last_Name, 
              ContactNo = cList.Contact_No, 
              GroupName = custGroup.Customer_Group_Name 
              }).ToList(); 
+0

우수. 이것은 내가 찾고 있었던 바로 그 것이다. – psyoptica

0

이 방법이 유용할까요?

private IEnumerable<Contacts> JoinTables(IEnumerable<Contact_List> contactLists, IEnumerable<CustomerGroup> customerGroups) 
{  
    return contactLists.Join(customerGroups, 
          x => x.Customer_Code, 
          y => y.Customer_Code, 
          (x, y) => new Contacts() 
          { 
           ContactNo = x.Contact_No, 
           FirstName = x.First_Name, 
           LastName = x.Last_Name, 
           GroupName = y.Customer_Group_Name 
          }); 
} 
+0

코드를 조금 설명 할 수 있습니까? – psyoptica

+0

위의 문맥에서 x와 y는 무엇입니까? – psyoptica

+0

이들은 [lambda expressions] (https://msdn.microsoft.com/en-us/library/bb397687.aspx)입니다. LINQ는 사실 lambdas에 관한 것입니다. :) 여기서'x => x.Customer_Code'는 열거 형 'contactLists' (여기서 요소는'Contact_List' 타입입니다)에서'Customer_Code' 속성을 접합 키로 사용한다는 것을 의미합니다. 그렇다면'y => y.Customer_Code'는 열거 형'customerGroups' (여기서 요소는'CustomerGroup' 유형입니다)에서 공동 키로 'Customer_Code' 속성을 가져 간다는 것을 의미합니다. – bashis

1

시도가 다음

  List<CustomerGroup> groups = new List<CustomerGroup>(); 
      List<Contact_List> contact_list = new List<Contact_List>(); 

      List<Contacts> contacts = (from g in groups 
             join c in contact_list on g.Customer_Code equals c.Customer_Code 
             select new { groupName = g.Customer_Group_Name, c }) 
             .Select(x => new Contacts() { 
              FirstName = x.c.First_Name, 
              LastName = x.c.Last_Name, 
              ContactNo = x.c.Contact_No, 
              GroupName = x.groupName 
             }).ToList(); 
+0

코드를 조금만 설명해 주실 수 있습니까? 나는이 줄을 이해하지 못한다 "select new {groupName = g.Customer_Group_Name, c})" –

+0

정확히 무엇을 말하는가. CustomerGroup 클래스의 값인 groupName과 동일한 Customer_Code를 가진 Contact_List 행인 'c'라는 새 개체가 만들어집니다. – jdweng

관련 문제