【问题标题】:check for object list if two objects which are having all the property values [duplicate]如果两个对象具有所有属性值,则检查对象列表[重复]
【发布时间】:2021-08-15 02:31:30
【问题描述】:

我有一个对象列表(List<Text>),其对象结构如下所示,

public class Text
{
    public double? AValue { get; set; }
    public double? BValue{ get; set; }
    public double? CValue{ get; set; }
}

我需要检查对象计数列表是否不超过one,如果我们持有的对象具有AValue,并且如果发现超过一个,我需要抛出异常

示例有效列表对象是

       var textList= new List<Text> // this is valid because it does not have objects which are holding BValue and CValue
        {
            new Text{ AValue = 34}
        }   

       var textList= new List<Text> // this is valid because it does not have and object that is holding `Avalue` property
        {
            new Text{ BValue = 78, Cvalue= 6},
            new Text{ BValue = 2, Cvalue= 4}
        }

无效对象是

      var textList= new List<Text>  // it is invalid because it is having both objects having `AValue` property and `BValue` and `CValue` property      
        {
            new Text{ AValue = 55},
            new Text{ BValue = 66, Cvalue=77}
        }

在无效对象情况下我需要通过异常

我是这样检查的,显然这是错误的

       if(textList.Count(a => a.AValue.Value != default) > 1)
        {
            throw new ArgumentException("message");
        }

谁能告诉我如何达到同样的效果

【问题讨论】:

  • @.EnigmaState 相同的文本,相同的代码,相同的问题 = ?。甚至不想像其他人一样阅读。
  • 不,它不是关于重复的,如果我发现两个文本对象,我需要抛出一个错误,一个有 Avalue,另一个有 BValue 和 CValue。
  • @OlivierRogier,如果您还不清楚,请告诉我,这就是我在这里添加额外文字的原因

标签: c# linq .net-core


【解决方案1】:

好的,我更新了我的答案。 和OP谈过之后,我觉得我一开始就理解错了。

问题是,OP 有一个带有可为空道具的文本列表。 但是,列表项都应该设置相同的值。 我们可以通过为每个项目检查每个属性的空值来检查列表项设置属性计数。 起初,我们只是将第一项作为我们期望有多少集合属性的参考。然后我们可以迭代列表并检查是否有任何实体与设置的属性计数不匹配。

 var textList = new List<Text>
        {
            new Text{ AValue = 34, BValue = 23 },
            new Text{ AValue = 24, BValue = 32 },
            new Text{ AValue = 32, BValue = 42 },
            new Text{ AValue = 23, BValue = 11 },
        }; // This List is valid

 var textList2 = new List<Text>
        {
            new Text{ AValue = 34, BValue = 23 },
            new Text{ AValue = 24, BValue = 32 },
            new Text{ AValue = 32 },
            new Text{ AValue = 23, BValue = 11 },
        }; // This List is not valid

        var numValuesSet = textList.FirstOrDefault()?.GetNumValuesSet() ?? 0;
        if (textList.Count(x => x.GetNumValuesSet() != numValuesSet) > 0)
        {
            throw new Exception();
        }
    

    public class Text
    {
        private static System.Reflection.PropertyInfo[] _properties = typeof(Text).GetProperties(); // Gets properties of type text (this type) and saves it

        public double? AValue { get; set; }
        public double? BValue { get; set; }
        public double? CValue { get; set; }

        public int GetNumValuesSet()
        {
            int count = 0;

            foreach (var prop in _properties)
            {
                if (prop.GetValue(this) != null) // Iterates all properties of this object and checks if it is null, if not we count up
                {
                    count++;
                }
            }

            return count;
        }
    }

这是正确的吗?

【讨论】:

  • 我只需要布尔变量我怎么能得到它
  • 什么意思?
  • 并且这个条件仍然有效CValue.HasValue() &amp;&amp; BValue.HasValue()
  • 您可以使用All 而不是Selectbool valid = MyList.All(x =&gt; x.IsValid()); 如果MyList 中的所有项目都有效,这将返回true。如果MyList为空,也会返回true
  • 它在其他一些情况下失败
【解决方案2】:

另一种方法是基于 List 创建自己的 List 类来覆盖 Add 方法。

public class Testing
{
    public double? AValue { get; set; }
    public double? BValue { get; set; }
    public double? CValue { get; set; }
}

public class MyList<Testing> : List<Testing>
{
    public new void Add(Testing item)
    {
        if (this != null && this.Count > 0) {
            var itemNullProps = item.GetType().GetProperties().Where(x => x.GetValue(item, null) == null).Select(x => x.Name);
            var thisNullProps = this.FirstOrDefault().GetType().GetProperties().Where(x => x.GetValue(this.FirstOrDefault(), null) == null).Select(x => x.Name);

            if (itemNullProps.Except(thisNullProps).Count() > 0)
                throw new ApplicationException("Not allowed");
        }
        base.Add(item);
    }
}

在您的实现中,您可以使用构造函数或 ADD 方法来检查并确保列表中的所有项目都包含已经具有值的属性的值。

// This is valid
MyList<Testing> newListx = new MyList<Testing>()
{
    new Testing{ BValue = 78, CValue= 6},
    new Testing{ BValue = 2, CValue= 4}
};

// This, of course, will throw error
MyList<Testing> newList2 = new MyList<Testing>()
{
    new Testing{ BValue = 78},
    new Testing{ BValue = 2, CValue= 4}
};

.. 除非我完全误读了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2013-12-26
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2020-06-08
    相关资源
    最近更新 更多