[英]Create object with special property if it does not exist yet
I have an OxyPlot
with an X and a Y axis where I want to change the maximum values several times.我有一个带有 X 轴和 Y 轴的OxyPlot
,我想在其中多次更改最大值。 To change them, I have to create the axes first.要更改它们,我必须先创建轴。
Is there a nicer way to edit for example the X-axis (AxisPosition.Bottom) if it exists and if not to create a new one?是否有更好的方法来编辑例如 X 轴 (AxisPosition.Bottom)(如果存在)以及是否创建新轴?
This is my code right now:这是我现在的代码:
if (opGraph.Axes.Any(s => s.Position == AxisPosition.Bottom))
{
OxyPlot.Wpf.Axis xAxis = opGraph.Axes.FirstOrDefault(s => s.Position == AxisPosition.Bottom);
xAxis.AbsoluteMaximum = absoluteMaximum;
}
else
{
opGraph.Axes.Add(new OxyPlot.Wpf.LinearAxis
{
Position = AxisPosition.Bottom,
AbsoluteMaximum = absoluteMaximum,
});
}
It's not necassary to first call Any
and then also FirstOrDefault
.不必先调用Any
然后再调用FirstOrDefault
。 This results in double iterations.这导致双重迭代。
The latter alone does the job:后者单独完成工作:
OxyPlot.Wpf.Axis xAxis = opGraph.Axes.FirstOrDefault(s => s.Position == AxisPosition.Bottom);
if (xAxis != null)
{
xAxis.AbsoluteMaximum = absoluteMaximum;
}
else
{
opGraph.Axes.Add(new OxyPlot.Wpf.LinearAxis
{
Position = AxisPosition.Bottom,
AbsoluteMaximum = absoluteMaximum,
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.