2014-10-28 73 views
0

假设有一个数据表如下:验证分层相似结构

enter image description here

每一行是(N1,N2,N3,N4)的组合,与该约束:

  • 只有N1,N2,N3和N4可以为空
  • 在每一行中,N(n)只有在N(n-1)为NULL(类似于层次结构)时才可以为NULL。
  • (N1,N2,N3,N4)的每个组合在整个集合内是唯一的。

我正在寻找一整套解决方案,“没有组合必须有一个值在它的数量列小于其子组合的总和”;。

例如行数:1必须大于行数总和:2,10,11,因此行数:2必须大于行数总和:3,4,5,6,7,8 ,9(当然在特定情况下无效)。

我的开发环境是C#.net,使用Linq是首选。

在此先感谢

+0

阙? - 我认为你需要更好地解释你的例子。否则,我希望一些比我聪明的人能够帮助你。 – Enigmativity 2014-10-28 09:35:54

+0

@Enigmativity;考虑一个由4级数据组成的预算树。 但在我的情况下,数据结构不是分层的(自引用),并且由4个固定列完成。我希望它有帮助 – 2014-10-28 09:41:08

+0

对不起,我不明白你的数据或你的例子。我认为您可能需要对数据进行全面的手动计算,并向我们展示工作。 – Enigmativity 2014-10-28 10:01:37

回答

0

你可以拉在一起决定了两个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)); 

当然预算条目的整个数据集,你不得不匹配每一个反对所有其他人喜欢笛卡尔产品。我不认为这很快,但它肯定应该完成这项工作。