【问题标题】:Validation on hierarchical alike structure分层相似结构的验证
【发布时间】:2014-10-28 09:30:40
【问题描述】:

假设有如下数据表:

每一行都是 (N1,N2,N3,N4) 的组合,具有以下约束:

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

我正在寻找一种解决方案,通过该解决方案,对于整个集合,“任何组合的金额列中的值都必须小于其子组合的总和”;。

例如 Amount of row:1 必须大于 sum of rows:2,10,11,因此 Amount of row:2 必须大于 sum of rows:3,4,5,6,7,8, 9(在给定的情况下当然是无效的)。

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

提前致谢

【问题讨论】:

  • ¿ - 我认为你需要更好地解释你的例子。否则我希望比我聪明的人能帮助你。
  • @Enigmativity;想象一个由 4 级数据组成的预算树。但在我的情况下,数据结构不是分层的(自引用),而是由 4 个固定列完成的。希望对你有帮助
  • 对不起,我不了解您的数据或示例。我认为您可能需要对您的数据进行完整的手动计算并向我们展示其工作原理。

标签: c# linq hierarchy


【解决方案1】:

您可以组合一个方法来决定两个类似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));

当然,对于整个预算条目数据集,您必须将每个条目与所有其他条目进行匹配,例如笛卡尔积。我不认为这很快,但它绝对可以完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多