简体   繁体   中英

query many to many relationship with Linq

I have a table named CustomerGroup which has a Many-Many relationship with the table contact_List. A third table CustomerGroupContact has the primary keys of both tables.

Here's what the CustomerGroup table looks like:

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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