我有代码,我尝试在QProduct
(lqp)usinq LINQ中通过productName
查找List<QProduct>
。我从文件名中获取的变量productNameInaccurate
通常包含一些通常位于字符串末尾的其他文本。例如,我从fileName获取的productName = '0/2'
和productNameInaccurate
是'0/2 new'
等。
我有这段代码:
//Get inaccurate product name from filename
productNameInaccurate = fileName.Substring(ind + 1, ind2 - ind - 1).Replace("-", "/");
//Get filtered list of products
List<QProduct> filtered = lqp.Where(x=>productNameInaccurate.StartsWith(x.Name, StringComparison.InvariantCultureIgnoreCase)).ToList();
//Some more filtering - here I need to get best match by productName
if (isDop)
qProduct = filtered.Where(x => x.normy.StartsWith("ČSN EN")).FirstOrDefault();
else
qProduct = filtered.Where(x => !x.normy.StartsWith("ČSN EN")).FirstOrDefault();
它运作正常,但我还有productName = '32/63'
和productName = '32/63 B I'
。即使QProduct
,此代码也会找到productName == '32/63'
{。}}。
我需要的是从productNameInaccurate == '32/63 BI'
列表中找到最佳匹配,以便QProduct
获得productNameInaccurate='0/2 new'
,QProduct.Name = '0/2'
我获得productNameInaccurate='32/63 Bi'
而不是QProduct.Name = '32/63 B II'
。理想情况下,按匹配字符数排序QProduct.Name = '32/63'
列表。
答案 0 :(得分:3)
“理想情况下,按匹配字符的数量排序筛选列表。”
// Get the filtered list and sort by count of matching characters
IEnumerable<QProduct> filtered = lqp
.Where(x=>productNameInaccurate.StartsWith(x.Name, StringComparison.InvariantCultureIgnoreCase))
.OrderByDesc(x => Fitness(x.ProductName, productNameInaccurate));
static int Fitness(string individual, string target) {
return Enumerable.Range(0, Math.Min(individual.Length, target.Length))
.Count(i => individual[i] == target[i]);
}