我正在使用IEqualityComparer
在使用LINQ to Entities的数据库中匹配“近似重复项”。
在大约40,000条记录集的情况下,此查询需要约15秒才能完成,我想知道是否可以对以下代码进行任何结构上的更改。
我的公共方法
public List<LeadGridViewModel> AllHighlightingDuplicates(int company)
{
var results = AllLeads(company)
.GroupBy(c => c, new CompanyNameIgnoringSpaces())
.Select(g => new LeadGridViewModel
{
LeadId = g.First().LeadId,
Qty = g.Count(),
CompanyName = g.Key.CompanyName
}).OrderByDescending(x => x.Qty).ToList();
return results;
}
获取潜在客户的私有方法
private char[] delimiters = new[] { ' ', '-', '*', '&', '!' };
private IEnumerable<LeadGridViewModel> AllLeads(int company)
{
var items = (from t1 in db.Leads
where
t1.Company_ID == company
select new LeadGridViewModel
{
LeadId = t1.Lead_ID,
CompanyName = t1.Company_Name,
}).ToList();
foreach (var x in items)
x.CompanyNameStripped = string.Join("", (x.CompanyName ?? String.Empty).Split(delimiters));
return items;
}
My IEqualityComparer
public class CompanyNameIgnoringSpaces : IEqualityComparer<LeadGridViewModel>
{
public bool Equals(LeadGridViewModel x, LeadGridViewModel y)
{
var delimiters = new[] {' ', '-', '*', '&', '!'};
return delimiters.Aggregate(x.CompanyName ?? String.Empty, (c1, c2) => c1.Replace(c2, '\0'))
== delimiters.Aggregate(y.CompanyName ?? String.Empty, (c1, c2) => c1.Replace(c2, '\0'));
}
public int GetHashCode(LeadGridViewModel obj)
{
var delimiters = new[] {' ', '-', '*', '&', '!'};
return delimiters.Aggregate(obj.CompanyName ?? String.Empty, (c1, c2) => c1.Replace(c2, '\0')).GetHashCode();
}
}