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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:45:40+00:00 2026-05-21T05:45:40+00:00

I have written some LINQ where I check against a database whether or not

  • 0

I have written some LINQ where I check against a database whether or not the username is unique when I am adding a new user. My code (see below) works, but is there a better way to do this? I am currently relying on trapping an error if the username does not exist.

try
        {
            var User = (from u in _database.Users
                        where u.UserID == strUserName
                        select u).First();

            if (User != null)
            {
                blnUnique = false;
            }
        }
        catch
        {
            blnUnique = false;
        }
  • 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-21T05:45:41+00:00Added an answer on May 21, 2026 at 5:45 am

    You can use the Enumerable.Any<TSource>() extension method:

    bool isUnique = (from u in database.Users
                     where u.UserID == userName
                     select u).Any();
    

    Here’s an alternative way of expressing this using a lambda:

    bool isUnique = database.Users.Any(u => u.UserID == userName);
    

    Note that the equals operator on the String class will make a case-sensitive ordinal comparison. If you wish to make a case-insensitive comparison you’ll have to use the String.Equals method:

    bool isUnique = database.Users.Any(
        u => u.UserID.Equals(userName, StringComparison.OrdinalIgnoreCase));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code in my VB.NET application to send an HTML e-mail
I'm just getting into unit testing, and have written some short tests to check
I have written a new custom component derived from TLabel. The component adds some
I have written some SAS code that calls R via the x command (I
I have written some code that pulls info from a plist file and does
I have written some code in C++ with a corresponding C interface (i.e C-function
I have written some matlab code for image analysis that searches for clusters in
I have written some code which sends queries to google and returns the query
I have written some CSS which targets elements using the parent > child selector.
I have some written a number of unit tests that test a wrapper around

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.