你基本上必须拿出一个公式来确定这两个对象有多相似。为每个属性选择一个权重,然后使用简单的比较来说明该属性是否应该被视为相同。可以使用某种类型的模糊匹配,但这会变得更加复杂。
简单的东西可能是:
public byte Similarity(SomeType other)
{
byte similarity = 0;
if (this.Property1 == other.Property1)
similarity += 25;
if (this.Property2 == other.Property2)
similarity += 13;
if (this.Property3 == other.Property3)
similarity += 12;
if (SomeFuzzyComparisonReturnsVerySimilar(this.Property4, other.Property4))
similarity += 50;
return similarity;
}
也就是说,我限定返回从0到100的数的简单方法; 100是相同的,0是完全不同的。
一旦你有了这些,选择足够相似的项目是一件相当简单的事情,你可以考虑;例如:
var similarObjects = ListOfSomeTypes.Where(s => s.Similarity(templateObject) > 75);
或者对它们进行排序:
var sortedBySimilarity = ListOfSomeTypes.OrderByDescending(s => s.Similarity(templateObject));
虽然最终我的观点是,你必须要拿出自己的“共同具有最有”的定义,一旦你有剩下的可能会很容易。这并不意味着这一定很容易。
随着你的问题的更多细节,一个可能的公式是:
public byte Similarity(A other)
{
byte similarity = 0;
if (this.b == null | other.b == null)
similarity += 25;
else if (this.b == other.b)
similarity += 50;
if (this.c == null | other.c == null)
similarity += 25;
else if (this.c == other.c)
similarity += 50;
return similarity;
}
这确切的重量在一个对象略显不足匹配最高,空值和分歧都没有。
我想你会给我们更多的细节,然后才有可能得到一个像样的答案。 – StriplingWarrior 2011-04-26 23:23:06
你能给出一个你如何使用这个功能的代码示例,向我们展示一下什么需要实现? – David 2011-04-26 23:26:27