简体   繁体   English

查询与Linq的多对多关系

[英]query many to many relationship with Linq

I have a table named CustomerGroup which has a Many-Many relationship with the table contact_List. 我有一个名为CustomerGroup的表,该表与表contact_List有许多关系。 A third table CustomerGroupContact has the primary keys of both tables. 第三个表CustomerGroupContact具有两个表的主键。

Here's what the CustomerGroup table looks like: 这是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; }

}

Here's what Contact_List Model looks like: 这是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; }

}

I'm trying to join the 2 tables to create an object that will look like the model below: 我正在尝试加入2个表来创建一个对象,该对象看起来像下面的模型:

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

I'm struggling to use the right query statement that will join the table based on customer_code property. 我正在努力使用正确的查询语句,该语句将基于customer_code属性加入表。 I'd appreciate any sort of help. 我将不胜感激。

Try following : 尝试以下操作:

            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();

Would this work? 这行得通吗?

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

Hope this would work: 希望这会起作用:

    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM