1

我有一个联系人列表,其中每个联系人(保存在联系人表中)可以有多个号码(保存在另一个表 Contact_Phones 中)

public class Contact{
    public int ID {get; set;}
    public string First_Name {get; set;}
    public string Last_Name {get; set;}
    public List<Contact_Phones> Phones {get; set;}
}

public class Contact_Phones{
    public int ID {get; set;}
    public int Type {get; set;}
    public string Phone_No {get; set;}
}

现在我想使用 Linq C# 搜索具有相同 phone_no 的联系人并将它们合并。

4

3 回答 3

1
var query= ContactsRepo.GetAll()
           .SelectMany(contact => contact.Phones)
           .GroupBy(contactPhone => contactPhone.Phone_No)
           .ToList();
于 2013-12-26T09:39:23.497 回答
0
var contactsWithduplicates = contacts
                     .Where(contact => contact.Phones.Any(phone => 
                             contacts.Any(contact2 => contac2 != contact && contact2.Phones.Contains(phone))))
                     .Distinct().ToList();
于 2013-12-26T10:33:22.240 回答
0

假设您在一个名为的列表中有您的联系人contacts

// Break into phones and contact id for example
var all_phones = from contact in contacts
            from phone in contact.Phones
            select new {contact_id = contact.ID, phone = phone.Phone_No};

// get only contacts that share the same phone 
var duplicates = from entries in all_phones
                 group entries by entries.phone 
                 into g where g.Count() >1
                 select g;
于 2013-12-26T10:37:04.587 回答