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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:52:09+00:00 2026-06-11T20:52:09+00:00

We have the following code to check if the given username and password exists

  • 0

We have the following code to check if the given username and password exists in database:

    public User GetUserByUserNameAndPassword(string userName, string userPassword)
    {
        using (var context = DataObjectFactory.CreateContext())
        {
            return Mapper.Map(context.UserEntities.Single(u => u.UserName == userName && u.UserPassword == userPassword));
        }
    }

If we have the username and password in database this works fine but throws an error if username or password is wrong and no records found.

This might be simple but as I am new to lambda could not get get it right.

How can I change the lambda query so we can handle nulls?

Thanks

  • 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-11T20:52:10+00:00Added an answer on June 11, 2026 at 8:52 pm

    Use SingleOrDefault, which will return only one record or null if none exists.

    return Mapper.Map(context.UserEntities.SingleOrDefault(u => u.UserName == userName && u.UserPassword == userPassword));
    

    Later you can check:

    User returnedObject = GetUserByUserNameAndPassword(username,password)
    
    if(returnedObject == null)
    {
    //User doesn't exist
    }
    

    Remember Single/SingleOrDefault will throw an exception if multiple records exist against the criteria.

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

Sidebar

Related Questions

I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I am using the code for remember my username and password. i have tried
I have the following code: $r->find('user')->via('post')->over(authenticated => 1); Given that route I can get
I have the following code which I'm using to get the user to input
I have the following codes to check md5 encrypted password against user input password:
I have the following PHP code // Check if the upload is setted if
I have the following piece of code to check for supported intents. The code
I have the following code: foreach (ListItem item in check.Items) { if (item.Selected) {
I have the following piece of code where check is for char type do{

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.