C# 使用Linq C查找具有相同电话号码的联系人#
我有一个联系人列表,其中每个联系人(保存在联系人表中)可以有多个号码(保存在另一个联系人表中)C# 使用Linq C查找具有相同电话号码的联系人#,c#,linq,entity-framework,linq-query-syntax,C#,Linq,Entity Framework,Linq Query Syntax,我有一个联系人列表,其中每个联系人(保存在联系人表中)可以有多个号码(保存在另一个联系人表中) 公共类联系人{ 公共int ID{get;set;} 公共字符串First_Name{get;set;} 公共字符串Last_Name{get;set;} 公共列表电话{get;set;} } 公共类联络电话{ 公共int ID{get;set;} 公共int类型{get;set;} 公共字符串电话号码{get;set;} } 现在,我想搜索具有相同电话号码的联系人(不使用Linq C),并将其合并
公共类联系人{
公共int ID{get;set;}
公共字符串First_Name{get;set;}
公共字符串Last_Name{get;set;}
公共列表电话{get;set;}
}
公共类联络电话{
公共int ID{get;set;}
公共int类型{get;set;}
公共字符串电话号码{get;set;}
}
现在,我想搜索具有相同电话号码的联系人(不使用Linq C),并将其合并。假设您在名为
联系人的列表中有联系人:
var query= ContactsRepo.GetAll()
.SelectMany(contact => contact.Phones)
.GroupBy(contactPhone => contactPhone.Phone_No)
.ToList();
// 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;
您想如何合并它们?你想要的最终输出是什么?禁用延迟加载后,它只会给你手机
var contactsWithduplicates = contacts
.Where(contact => contact.Phones.Any(phone =>
contacts.Any(contact2 => contac2 != contact && contact2.Phones.Contains(phone))))
.Distinct().ToList();
// 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;