2014-10-28 28 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)); 

當然預算條目的整個數據集,你不得不匹配每一個反對所有其他人喜歡笛卡爾產品。我不認爲這很快,但它肯定應該完成這項工作。