I have a method that can be written fairly neatly through a chain of methods:
return viewer.ServerReport.GetParameters() .Single(p => p.Name == Convention.Ssrs.RegionParamName) .ValidValues .Select(v => v.Value);
However, I would like to be able to do some checks at every point, because I want to provide useful diagnostic information if any of the chained methods returns unexpected results.
To achieve this, I need to break the whole chain and complete each call with an if
block. This makes the code much less readable.
Ideally, I would like to be able to interlace calls with chains that allow me to handle unexpected results at each point (for example, throw a meaningful exception such as new ConventionException("The report contains no parameter")
if the first method returns an empty collection). Can anyone suggest an easy way to achieve this?
Edit:
This is the result of using @JeffreyZhao's answer:
return viewer.ServerReport.GetParameters() .Assert(result => result.Any(), "The report contains no parameter") .SingleOrDefault(p => p.Name == Convention.Ssrs.RegionParamName) .Assert(result => result != null, "The report does not contain a region parameter") .ValidValues .Select(v => v.Value) .Assert(result => result.Any(), "The region parameter in the report does not contain any valid value");
source share