您可以组合一个方法来决定两个类似Tuple<N1,N2,N3,N4> 的对象的父子关系。想法:位数组表示和移位。使用这个幼稚的模型:
public class Budget
{
public int Id { get; set; }
//
public int N1 { get; set; }
public Nullable<int> N2 { get; set; }
public Nullable<int> N3 { get; set; }
public Nullable<int> N4 { get; set; }
//
public float Amount { get; set; }
/// <summary>
/// Method analyzes if current object is a parent of <paramref name="other"/>
/// if you override GetHashCode or provide a nifty bit array representation
/// you can infer parent-child relationships with really fast bit shifting
/// </summary>
/// <param name="other">budget to compare with</param>
public bool IsParentOf (Budget other)
{
// ommitted for too-time-consuming and 'your work obviously'
// or 'not-the-purpose-of-this-site'reasons
return true;
}
}
您可以尝试为每个预算条目获取子组合(此处为您的分类):
Budget b1 = new Budget() { N1 = 1, N2 = null, N3 = null, N4 = null, Amount = 1200f };
Budget b11 = new Budget() { N1 = 1, N2 = 1, N3 = null, N4 = null, Amount = 800f };
Budget b111 = new Budget() { N1 = 1, N2 = 1, N3 = 1, N4 = null, Amount = 800f };
Debug.Assert (b1.IsParentOf(b11));
Debug.Assert(b1.IsParentOf(b111));
Debug.Assert(b11.IsParentOf(b111));
var budgetEntries = new List<Budget>() { b11, b111 };
var subCombinations = budgetEntries.Where(be => b1.IsParentOf(be));
Debug.Assert(b1.Amount > subCombinations.Sum(sc => sc.Amount));
当然,对于整个预算条目数据集,您必须将每个条目与所有其他条目进行匹配,例如笛卡尔积。我不认为这很快,但它绝对可以完成这项工作。