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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:25:27+00:00 2026-05-11T20:25:27+00:00

I would like to check if an entity is already added to the database.

  • 0

I would like to check if an entity is already added to the database. So, how can I see this difference between a and b?

var a = dataContext.Things.First(x => x.Name == something);
var b = new Thing { Name = something };

To make it clearer, if I have this:

var thing = dataContext.Things.FirstOrDefault(x => x.Name == something) 
            ?? new Thing { Name = something };

How can I see if thing needs to be inserted?

  • 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-11T20:25:27+00:00Added an answer on May 11, 2026 at 8:25 pm

    If you use FirstOrDefault instead of First, that will return null if there are no matches.

    As for knowing whether you need to insert – just remember whether or not it was null to start with:

    var a = dataContext.Things.FirstOrDefault(x => x.Name == something);
    bool needsInsertion = (a == null);
    a = a ?? new Thing { Name = something }; 
    

    Alternatively, if there’s an ID field in Thing which is automatically populated by the database, you can just use that to detect whether it’s already in the database or not.

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

Sidebar

Related Questions

Say I have a table products and I would like to check if this
I would like to know whether there is a jQuery function which can check
My forms have checkboxes and I would like to replace the normal arrow check
I would like to update an open-source war file in Maven with some additional
I would like to massively use inline in my project to speed up performance.
I would like to have a counter, that counts backwards, and is stopped if
Update the question Hello All, I would like improve my email address validation code.
when i try to attach entity to context i get an exception An object
I want to write generic method that checks if a given entity is in
I'm having some trouble with Instruments. My app is working perfectly without crashes, but

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.