I have the following code:
if (isMandatory)
{
x.MandatoryServices.ForEach(y =>
{
if (!y.IsSelectedByUser)
{
containsRemovedSrvcs = true;
return;
}
});
}
else
{
x.OptionalServices.ForEach(y =>
{
if (!y.IsSelectedByUser)
{
containsRemovedSrvcs = true;
return;
}
});
}
I would like to replace it with something more elegant like:
x.{isMandatory ? MandatoryServices : OptionalServices}.ForEach(y =>
{
if (!y.IsSelectedByUser)
{
containsRemovedSrvcs = true;
return;
}
});
Of course, the compiler doesn't like that. I have seen the GetType().GetProperty().GetValue()
approach but I don't really know if that will work since my property is a List. Does anyone know of any better solutions?