2016-11-24 169 views
0

我有一個名爲CustomerGroup的表,它與表contact_List具有多對多關係。第三個表CustomerGroupContact具有兩個表的主鍵。與Linq查詢多對多關係

這裏的CustomerGroup表的樣子:

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型號的樣子:

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

} 

我想加入2個表來創建一個對象,看起來像以下型號:

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表達式](https://msdn.microsoft.com/en-us/library/bb397687.aspx)。實際上,LINQ就是lambda表達式:)這裏'x => x.Customer_Code'表示我們把屬性'Customer_Code'作爲可枚舉的'contactLists'(其中元素的類型爲'Contact_List')的聯合關鍵字。那麼'y => y.Customer_Code'意味着我們也從屬性'customer_Code'中作爲可枚舉的'customerGroups'(其中元素的類型爲'CustomerGroup')的聯合關鍵字。 – 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

它的確如它所說的那樣。使用groupName作爲CustomerGroup類的值創建一個新對象,'c'爲具有相同Customer_Code的Contact_List行。 – jdweng