C# 如果两个对象具有所有特性值,请检查对象列表
我有一个对象列表(C# 如果两个对象具有所有特性值,请检查对象列表,c#,linq,.net-core,C#,Linq,.net Core,我有一个对象列表(list),对象结构如下所示 public class Text { public double? AValue { get; set; } public double? BValue{ get; set; } public double? CValue{ get; set; } } 我需要检查对象计数列表是否不超过一个,如果我们持有的对象具有AValue,如果找到多个,我需要抛出异常 示例有效列表对象为 var textList= ne
list
),对象结构如下所示
public class Text
{
public double? AValue { get; set; }
public double? BValue{ get; set; }
public double? CValue{ get; set; }
}
我需要检查对象计数列表是否不超过一个
,如果我们持有的对象具有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}
}
有人能告诉我如何才能达到同样的效果吗好的,我更新了我的答案。
和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;
}
}
var textList=新列表
{
新文本{AValue=34,BValue=23},
新文本{AValue=24,BValue=32},
新文本{AValue=32,BValue=42},
新文本{AValue=23,BValue=11},
}; // 此列表有效
var textList2=新列表
{
新文本{AValue=34,BValue=23},
新文本{AValue=24,BValue=32},
新文本{AValue=32},
新文本{AValue=23,BValue=11},
}; // 此列表无效
var numValuesSet=textList.FirstOrDefault()?.GetNumValuesSet()??0;
if(textList.Count(x=>x.GetNumValuesSet()!=numValuesSet)>0)
{
抛出新异常();
}
公共类文本
{
private static System.Reflection.PropertyInfo[]_properties=typeof(Text).GetProperties();//获取Text类型(此类型)的属性并保存它
公共双?AValue{get;set;}
公共双?b值{get;set;}
公共双精度?C值{get;set;}
public int GetNumValuesSet()
{
整数计数=0;
foreach(属性中的var属性)
{
if(prop.GetValue(this)!=null)//迭代此对象的所有属性,并检查它是否为null,如果不是,则进行计数
{
计数++;
}
}
返回计数;
}
}
这是否正确?另一种方法是基于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);
}
}
公共类测试
{
公共双?AValue{get;set;}
公共双?b值{get;set;}
公共双精度?C值{get;set;}
}
公共类MyList:列表
{
公共新增作废(测试项目)
{
if(this!=null&&this.Count>0){
var itemNullProps=item.GetType().GetProperties()。其中(x=>x.GetValue(item,null)==null)。选择(x=>x.Name);
var thisNullProps=this.FirstOrDefault().GetType().GetProperties()。其中(x=>x.GetValue(this.FirstOrDefault(),null)==null)。选择(x=>x.Name);
if(itemNullProps.Except(thisNullProps.Count()>0)
抛出新的ApplicationException(“不允许”);
}
基础。添加(项目);
}
}
在您的实现中,您可以使用构造函数或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}
};
//这是有效的
MyList newListx=新MyList()
{
新测试{BValue=78,CValue=6},
新测试{BValue=2,CValue=4}
};
//当然,这会带来错误
MyList newList2=新MyList()
{
新测试{BValue=78},
新测试{BValue=2,CValue=4}
};
。。除非我完全误读了这个问题。@。谜陈述相同的文本,相同的代码,相同的问题=?。我甚至不想读一个像另一个一样。不,这不是关于重复,如果我发现两个文本对象,一个是Avalue,另一个是BValue和CValue,我需要抛出一个错误。@OlivierRogier,如果你仍然不清楚,请告诉我,这就是为什么我在这里添加了额外的文本,我只需要一个布尔变量,我怎么能得到它?你的意思是什么?这个条件仍然有效
CValue.HasValue()&&BValue.HasValue()
,而不是Select
,你可以使用它bool valid=MyList.All(x=>x.IsValid())
如果MyList
中的所有项目都有效,则返回true
。如果MyList
为空,它也将返回true
。对于某些其他情况,它将失败
// 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}
};