I'm using the following IEqualityComparer to strip special characters from a company name before comparison as follows:
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();
}
}
In order to call this when running a query, I use the following:
var results = result
.GroupBy(c => c, new CompanyNameIgnoringSpaces())
.Select(g => new LeadGridViewModel
{
LeadId = g.First().LeadId,
Qty = g.Count(),
CompanyName = g.Key.CompanyName,
}).OrderByDescending(x => x.Qty).ThenBy(x => x.CompanyName).ToList();
How would I use this comparer in a LINQ query in order to find all records which match the input string (company name)?
For example:
public List<LeadGridViewModel> AllByName(string name, int company)
{
var result = (from t1 in db.Leads
where
t1.Company_ID == company && t1.Company_Name == name...
So instead of using t1.Company_Name == name, I'd like to use use the equality comparer for this to include the special character replacement.