Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:25:54+00:00 2026-06-05T08:25:54+00:00

I have a method which can be written pretty neatly through method chaining: return

  • 0

I have a method which can be written pretty neatly through method chaining:

return viewer.ServerReport.GetParameters()
    .Single(p => p.Name == Convention.Ssrs.RegionParamName)
    .ValidValues
    .Select(v => v.Value);

However I’d like to be able to do some checks at each point as I wish to provide helpful diagnostics information if any of the chained methods returns unexpected results.

To achieve this, I need to break up all my chaining and follow each call with an if block. It makes the code a lot less readable.

Ideally I’d like to be able to weave in some chained method calls which would allow me to handle unexpected outcomes at each point (e.g. throw a meaningful exception such as new ConventionException("The report contains no parameter") if the first method returns an empty collection). Can anyone suggest a simple way to achieve such a thing?

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");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:25:55+00:00Added an answer on June 5, 2026 at 8:25 am

    Maybe you can use this approach.

    static T Check<T>(this T value)
    {
        if (...) throw ...;
    
        return value;
    }
    

    then:

    xxx.Single(...).Check().Select(...).Check()...
    

    Update:

    You can even:

    static T Validate<T>(this T value, Func<T, bool> validate, string errorMessage)
    {
        if (!validate(value)) 
            throw new ValidationFailedException(errorMessage);
    
        return value;
    }
    

    then:

    xxxx.Single()
        .Validate(v => v > 0, "Must be greater than zero")
        .NextStep()
        .Validate(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a boolean method which can be called upon to read information
I have an extension method which I can use from the .cs codebehind of
I have recently begun using Scala. I've written a DSL in it which can
I have written a method which writes an object to file. I used generics
I have written a helper class which uses the Action - delegate as method
I have a small scripting engine written which can run basic code. I want
I have a piece of unix software written in C++ which can be build
I have written a perl script to access a wcf service method which returns
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which I would like to invoke asynchronously: void Foo() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.