C# 查找一个枚举中的字段是否存在于另一个枚举的两个可能字段中
我有两个不同类型的枚举数C# 查找一个枚举中的字段是否存在于另一个枚举的两个可能字段中,c#,linq,ienumerable,C#,Linq,Ienumerable,我有两个不同类型的枚举数 public Contact { public string fullName; } public Property { public string Rep; public string PropMgr; } 我正在尝试获取在Rep或PropMgr字段中表示的所有“联系人” 我的直觉是,在rep上加入一个结果,然后在PropMgr上加入另一个结果集。然后加入结果集并选择distinct。不确定这是否会奏效,也不确定是否有更有效的方法 添加一
public Contact
{
public string fullName;
}
public Property
{
public string Rep;
public string PropMgr;
}
我正在尝试获取在Rep或PropMgr字段中表示的所有“联系人”
我的直觉是,在rep上加入一个结果,然后在PropMgr上加入另一个结果集。然后加入结果集并选择distinct。不确定这是否会奏效,也不确定是否有更有效的方法
添加一些附加信息: 一些数据可能是
Contact
FullName: Nick
Property
Name: "Happy Place"
PropMgr: Nick
Rep: Sally
当比较这两组,我得到这个组合,我想选择接触“尼克”
请记住,我有IEnumerable Contacts列表和IEnumerable Property列表请尝试以下Linq查询
Contacts.Select(x=>x.fullName).Intersect(Properties.Select (x=>x.Rep).Union(Properties.Select(x=>x.PropMgr))
// contacts -> IEnumerable of Contact, Properties -> IEnumerable of Property
我没有任何数据来测试它,但我想类似的东西应该可以工作:
IEnumerable<Contact> contacts = ...
IEnumerable<Property> properties = ...
var query = from property in properties
from name in new[] { property.Rep, property.PropMgr }
join contact in contacts on name equals contact.FullName
select contact;
var result = query.Distinct().ToList();
IEnumerable contacts=。。。
IEnumerable属性=。。。
var query=来自属性中的属性
来自新[]{property.Rep,property.PropMgr}中的名称
在姓名等于contact.FullName的联系人中加入联系人
选择联系人;
var result=query.Distinct().ToList();
解决同一问题的方法有很多种
尝试一些技术含量较低的东西:
var namesInProperties = new HashSet<string>();
foreach (Property p in PropertyList)
{
namesInProperties.Add(p.PropMgr);
namesInProperties.Add(p.Rep);
}
IEnumerable<Contact> matchingContacts = ContactsList.Where(c => namesInProperties.Contains(c.fullName));
var namesInProperties=new HashSet();
foreach(PropertyList中的属性p)
{
名称属性。添加(p.PropMgr);
名称属性。添加(p.Rep);
}
IEnumerable matchingContacts=ContactsList.Where(c=>namesInProperties.Contains(c.fullName));
您的意思是fullName
值等于Rep
或PropMgr
属性值?是的。可能是这样,我可能误解了这个问题,但事实并非如此。在哪里(fullName==Rep | fullName==PropMgr)解决了这个问题?如果我将一个联系人与多个属性进行比较,这会起作用。但我将多个联系人与多个属性进行比较。除非我遗漏了什么。啊,是的,你也知道。谢谢你的帮助!