1

各連絡先 (Contact テーブルに保存) に複数の番号 (別のテーブル 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 に答える