【发布时间】: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,如果您还不清楚,请告诉我,这就是我在这里添加额外文字的原因