我有两张桌子,每张都有自己的型号...
class FamilyMan
{
public int family_ID {get; set;}
public string name {get; set;}
public string fav_color {get; set;}
}
class BusinessMan
{
public int work_ID {get; set;}
public string name {get; set;}
public string fav_color {get; set;}
//unrelated data etc
public string job_title {get; set;}
}
...并且我希望能够将所有 FamilyMans 与基于name
和的匹配 BusinessMans 匹配fav_color
。
我目前有类似的东西:
//fill lists from database
var family_list = dbContext.FamilyMen.ToList();
var busy_list = dbContext.BusinessMen.ToList();
//create empty dict for matching the two types
var matches = new Dict<FamilyMan, BusinessMan>();
foreach (FamilyMan fam_man in family_list) {
foreach (BusinessMan busy_man in busy_list) {
//if both names and colors match, consider them a matching
//object and add them each to the dict
if (fam_man.name == busy_man.name &&
fam_man.color == busy_man.color) {
matches_MtO[fam_man] = busy_man;
}
}
}
但它需要相当长的时间才能完成。
我还研究过用 a 循环一个列表,foreach
然后使用 LINQs FirstOrDefault 来匹配它们,但效率似乎差不多。
有没有更好的方法来匹配FamilyMan
s 和BusinessMan
s ?