检查两个对象是否具有所有属性值的对象列表 [英] check for object list if two objects which are having all the property values

查看:48
本文介绍了检查两个对象是否具有所有属性值的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have list of objects (List<Text>) with an object structure looks like as below,

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

如果我们拥有的对象具有 AValue ,并且我需要抛出异常,我需要检查对象列表是否不超过一个多于一个

I need to check if the list of object count not to exceed one if we have the object that is holding that has AValue and i need to throw exception if more than one found

示例有效列表对象是

       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}
        }

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

In invalid object case i need to through the exception

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

and i am checking like this and obviously it is wrong

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

任何人都可以让我知道如何实现同样的目标

Could any one please let me know how can i achieve the same

推荐答案

好,我更新了答案.与OP交谈后,我认为我一开始就理解错误的问题.

Ok, I updated my answer. After talking with OP, I think I understood the problem wrong at first.

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

The problem is, the OP has a List of Text with nullable props. However, the lists items should all have the same values set. We can check the lists items set property count by checking each property for a null value, for each item. At first we just take the first item as a reference on how many set properties we expect. Then we can iterate the list and check if any entities do not match the set properties count.

 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;
        }
    }

这正确吗?

这篇关于检查两个对象是否具有所有属性值的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆