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 8776205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:59:46+00:00 2026-06-13T18:59:46+00:00

Say I have some statements like: List<string> list = new List<string> {1, 1, 2,

  • 0

Say I have some statements like:

List<string> list = new List<string> {"1", "1", "2", "3", "4"};
try
{
    Class1 c = new Class1
        {
            s1 = list.Single(s => s == "1"),
            s2 = list.Single(s => s == "2"),
            s3 = list.Single(s => s == "3"),
            s4 = list.Single(s => s == "4")
        };
}
catch (InvalidOperationException ex)
{
    Console.WriteLine(ex.Message);
}

And I definitely will go to the catch block with an error “The input sequence contains more than one element” at this line:

s1 = list.Single(s => s == "1")

So, I just curious, it there any way to display error predicate in catch block? This will be very useful to fix the bugs, if we’ll see something like “There was a duplicate element “1” in sequence.” or even full predicate in string format. Can I somehow view this information and display or log it?

  • 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-13T18:59:48+00:00Added an answer on June 13, 2026 at 6:59 pm

    If you really need info about predicate and it’s parameters, then you can create your own Single extension method (it will be called instead of default method), which will actually wrap default Single call, but it will receive Expression (expression tree that represents the lambda expression) instead of Func (lambda expression):

    public static T Single<T>(this IEnumerable<T> sequence, 
                              Expression<Func<T, bool>> predicate)
    {
        try
        {
            return sequence.Single(predicate.Compile());
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine("Error on predicate " + predicate);
            throw;
        }            
    }
    

    On line s1 = list.Single(s => s == "1") it will throw an exception and write pretty error message:

    Error on predicate s => s == "1"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some methods that return a List of T like say GetAllEvents. In
Say you have some boiler plate using statements. Say something like this: #if !NUNIT
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Say I have some 10 categories that I need to reference in a web
If say I have some generic class, for example: public class Attribute<T> { }
Let say i have some words AB, AAB, AA. AB is not a prefix
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Let's say I have some model objects that resemble this: public class FooModel {

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.