表示看不懂的说。。
A,AS,Aa1,B,Bs,BBs12,34。。好凌乱。

解决方案 »

  1.   


    Ass 是 List<As>,而As是List<A>, B也是这样的情况, 小写的都是实例
      

  2.   

    As又不是类型,哪来List<As>……
      

  3.   

    数据结构上用Dictionary来放属性集合会方便一些。排版也比较清楚。static class Program
    {
        static void Main(string[] args)
        {
            List<Constraint> constraints = new List<Constraint>()
            {
                new Constraint("Height", Rule.GreaterThan, 100),
                new Constraint("Name", Rule.Equals, "csdn"),
            };        Dictionary<string, object> p1 = new Dictionary<string, object>()
            {
                {"Height", 110},
                {"Name", "csdn"},
                {"Volume", 280},
                {"Color", "red"},
            };        Dictionary<string, object> p2 = new Dictionary<string, object>()
            {
                {"Height", 80},
                {"Name", "csdn"},
                {"Volume", 300},
                {"Color", "red"},
            };        List<Dictionary<string, object>> inputs = new List<Dictionary<string, object>>(){p1, p2};
            List<Dictionary<string, object>> validEntities = inputs.Where(x => constraints.All(c => c.Match(x))).ToList();
            System.Diagnostics.Debug.Assert(validEntities.Count == 1 && validEntities[0] == p1); // 验证,只有p1符合要求
    }
    enum Rule
    {
        Equals,
        GreaterThan,
        LessThan,
    }class Constraint
    {
        public Constraint(string name, Rule rule, object value)
        {
            this.PropertyName = name; this.Rule = rule; this.Value = value;
        }
        public string PropertyName { get; private set; }
        public Rule Rule { get; private set; }
        public object Value { get; private set; }
        public bool Match(IDictionary<string, object> bag)
        {
            if (bag != null && bag.ContainsKey(this.PropertyName))
            {
                int diff = System.Collections.Comparer.Default.Compare(bag[PropertyName], this.Value);
                if (diff == 0 && this.Rule == Rule.Equals) return true;
                if (diff < 0 && this.Rule == Rule.LessThan) return true;
                if (diff > 0 && this.Rule == Rule.GreaterThan) return true;
            }
            return false;
        }
    }
      

  4.   


    学习了,这个排版太好了。 分都给你吧。
    另外我已经通过循环套循环的方法, 把List都遍历进行了比对, 然后输出了结果 Dictionary确实很好用, 但我不会把这个作为Class定义 在大型project里面不知道怎么作为一个独立出来的能随时修改的部分进行定义呢?