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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:20+00:00 2026-05-28T02:46:20+00:00

I was searching for a solution to permform a linq query and ignore case.

  • 0

I was searching for a solution to permform a linq query and ignore case. I found this:

m_context.Users.SingleOrDefault(u => string.Compare(u.UserName, username, StringComparison.InvariantCultureIgnoreCase) == 0);

It search for a user object based on the username provided, ignoring case. It works, that’s not the question here, but when analysing the code it seems strange to me. I mean, inside the linq, we have the string.Compare(…,…,…) returning an integer. So what? How is it managed by linq (SingleOrDefault)?

Thanks for your help.

  • 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-05-28T02:46:21+00:00Added an answer on May 28, 2026 at 2:46 am

    You are passing a predicate into the SingleOrDefault method. The predicate evaluates to true or false, and this method returns the single element in the sequence that satisfies that predicate.

    u => string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase) == 0
    

    This is a Func<User, bool> predicate, which means it is a function that accepts a User as an argument u and returns a boolean value as a result of the string.Compare(...) == 0 evaluation. The single element in the sequence of users to satisfy this condition is then returned. If more than one satisfies the predicate, it is an error. If less than one satisfies the predicate, you get the default value for the type, which for a reference type is simply null.

    Think of it as very roughly

    public static T SingleOrDefault<T>(this IEnumerable<T> sequence, Func<T, bool> predicate)
    {
         T foundItem = null;
         int count = 0;
    
         foreach (T item in sequence)
         {
              if (predicate(item)) // evaluates the u => string.Compare(...)
              {
                   count += 1;
                   if (count > 1)
                       throw new InvalidOperationException("...");
    
                   foundItem = item;
              }
         }
    
         return foundItem;
    }
    

    The above is again just my rough draft of what the method does, not the actual implementation. If you’re interested in a more in-depth investigation of linq-to-objects implementations, consider reading Jon Skeet’s Edulinq series, where he goes through and reimplements every (give or take) method and explains it along the way. Again, that’s not the actual source code of the library, but it is very educational.

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

Sidebar

Related Questions

While searching for a solution to a python regular expression problem I found this
I've been searching for a solution on the internet for this, and can not
After searching online, the best solution I've found so far is to just make
Hi I have been searching a solution to this problem for many days but
I'm not sure that's the right plattform asking this question. I'm searching a solution
I've literally spent the past half hour searching for the solution to this, and
(This is not a question per se, I'm documenting a solution I found using
I have done a bit of searching for a solution to wrap text around
While searching the interweb for a solution for my VB.net problems I often find
I tried searching here for a similar solution but didn't see one so I

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.