C# if element not found, then something else












1















I have a simple table



enter image description here



Here's a snippet of my code.



var person = db.Person.Where(p => p.ID == inputId).Select(o => o.ID).FirstOrDefault();


return person;



inputId is a user input field. What I need is if the inputId is not in the table (eg. 5, 6, 7, ect), I want an if/else option to create a new user/reenter a correct id.



Thanks










share|improve this question




















  • 3





    So what are you waiting for?

    – Just code
    Nov 22 '18 at 7:15











  • Check if any exist then db.Person.Any(p => p.ID == inputId)

    – Marcus Höglund
    Nov 22 '18 at 7:16











  • if person is null then that is your else statement

    – brykneval
    Nov 22 '18 at 7:16






  • 1





    so you have heard of the if/else construct but do not know how to use it?

    – JohnB
    Nov 22 '18 at 7:19













  • That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

    – oerkelens
    Nov 22 '18 at 7:22
















1















I have a simple table



enter image description here



Here's a snippet of my code.



var person = db.Person.Where(p => p.ID == inputId).Select(o => o.ID).FirstOrDefault();


return person;



inputId is a user input field. What I need is if the inputId is not in the table (eg. 5, 6, 7, ect), I want an if/else option to create a new user/reenter a correct id.



Thanks










share|improve this question




















  • 3





    So what are you waiting for?

    – Just code
    Nov 22 '18 at 7:15











  • Check if any exist then db.Person.Any(p => p.ID == inputId)

    – Marcus Höglund
    Nov 22 '18 at 7:16











  • if person is null then that is your else statement

    – brykneval
    Nov 22 '18 at 7:16






  • 1





    so you have heard of the if/else construct but do not know how to use it?

    – JohnB
    Nov 22 '18 at 7:19













  • That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

    – oerkelens
    Nov 22 '18 at 7:22














1












1








1








I have a simple table



enter image description here



Here's a snippet of my code.



var person = db.Person.Where(p => p.ID == inputId).Select(o => o.ID).FirstOrDefault();


return person;



inputId is a user input field. What I need is if the inputId is not in the table (eg. 5, 6, 7, ect), I want an if/else option to create a new user/reenter a correct id.



Thanks










share|improve this question
















I have a simple table



enter image description here



Here's a snippet of my code.



var person = db.Person.Where(p => p.ID == inputId).Select(o => o.ID).FirstOrDefault();


return person;



inputId is a user input field. What I need is if the inputId is not in the table (eg. 5, 6, 7, ect), I want an if/else option to create a new user/reenter a correct id.



Thanks







c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:14









Just code

9,90752966




9,90752966










asked Nov 22 '18 at 7:13









MBrewersMBrewers

545




545








  • 3





    So what are you waiting for?

    – Just code
    Nov 22 '18 at 7:15











  • Check if any exist then db.Person.Any(p => p.ID == inputId)

    – Marcus Höglund
    Nov 22 '18 at 7:16











  • if person is null then that is your else statement

    – brykneval
    Nov 22 '18 at 7:16






  • 1





    so you have heard of the if/else construct but do not know how to use it?

    – JohnB
    Nov 22 '18 at 7:19













  • That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

    – oerkelens
    Nov 22 '18 at 7:22














  • 3





    So what are you waiting for?

    – Just code
    Nov 22 '18 at 7:15











  • Check if any exist then db.Person.Any(p => p.ID == inputId)

    – Marcus Höglund
    Nov 22 '18 at 7:16











  • if person is null then that is your else statement

    – brykneval
    Nov 22 '18 at 7:16






  • 1





    so you have heard of the if/else construct but do not know how to use it?

    – JohnB
    Nov 22 '18 at 7:19













  • That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

    – oerkelens
    Nov 22 '18 at 7:22








3




3





So what are you waiting for?

– Just code
Nov 22 '18 at 7:15





So what are you waiting for?

– Just code
Nov 22 '18 at 7:15













Check if any exist then db.Person.Any(p => p.ID == inputId)

– Marcus Höglund
Nov 22 '18 at 7:16





Check if any exist then db.Person.Any(p => p.ID == inputId)

– Marcus Höglund
Nov 22 '18 at 7:16













if person is null then that is your else statement

– brykneval
Nov 22 '18 at 7:16





if person is null then that is your else statement

– brykneval
Nov 22 '18 at 7:16




1




1





so you have heard of the if/else construct but do not know how to use it?

– JohnB
Nov 22 '18 at 7:19







so you have heard of the if/else construct but do not know how to use it?

– JohnB
Nov 22 '18 at 7:19















That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

– oerkelens
Nov 22 '18 at 7:22





That's a verbose way to just check if an ID exists... also naming a variable person, but putting only an ID in it is a good way to confuse your fellow programmers ;)

– oerkelens
Nov 22 '18 at 7:22












4 Answers
4






active

oldest

votes


















5














The null-coalescing operator is one way



var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();





share|improve this answer

































    2














    First of all, you return the ID, not the person. To return a person, you should do this:



    var person = db.Person.FirstOrDefault(p => p.ID == inputId);


    As for the question, I would prefer this option:



    if (db.Person.FirstOrDefault(p => p.ID == inputId) is Person person)
    {
    //...
    }





    share|improve this answer































      0














      first select record



      var person = db.Person.FirstOrDefault(p => p.ID == inputId);
      if(person == null) //check if not exist
      {
      //add
      db.Person.Add(PersonDetails);
      db.SaveChanges();
      }
      // else return person object
      return person;





      share|improve this answer

































        -1














        You can check existence of record with .Any() method:



        if(db.Person.Any(p => p.ID == inputID))
        {
        return db.Person.First(p => p.ID == inputID);
        }
        else
        {
        //Set user notification message
        }





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425622%2fc-sharp-if-element-not-found-then-something-else%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          The null-coalescing operator is one way



          var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();





          share|improve this answer






























            5














            The null-coalescing operator is one way



            var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();





            share|improve this answer




























              5












              5








              5







              The null-coalescing operator is one way



              var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();





              share|improve this answer















              The null-coalescing operator is one way



              var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 7:32









              Jannik

              1,05621844




              1,05621844










              answered Nov 22 '18 at 7:18









              fubofubo

              29.6k966104




              29.6k966104

























                  2














                  First of all, you return the ID, not the person. To return a person, you should do this:



                  var person = db.Person.FirstOrDefault(p => p.ID == inputId);


                  As for the question, I would prefer this option:



                  if (db.Person.FirstOrDefault(p => p.ID == inputId) is Person person)
                  {
                  //...
                  }





                  share|improve this answer




























                    2














                    First of all, you return the ID, not the person. To return a person, you should do this:



                    var person = db.Person.FirstOrDefault(p => p.ID == inputId);


                    As for the question, I would prefer this option:



                    if (db.Person.FirstOrDefault(p => p.ID == inputId) is Person person)
                    {
                    //...
                    }





                    share|improve this answer


























                      2












                      2








                      2







                      First of all, you return the ID, not the person. To return a person, you should do this:



                      var person = db.Person.FirstOrDefault(p => p.ID == inputId);


                      As for the question, I would prefer this option:



                      if (db.Person.FirstOrDefault(p => p.ID == inputId) is Person person)
                      {
                      //...
                      }





                      share|improve this answer













                      First of all, you return the ID, not the person. To return a person, you should do this:



                      var person = db.Person.FirstOrDefault(p => p.ID == inputId);


                      As for the question, I would prefer this option:



                      if (db.Person.FirstOrDefault(p => p.ID == inputId) is Person person)
                      {
                      //...
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 7:39









                      Stanislav MolchanovskyStanislav Molchanovsky

                      467116




                      467116























                          0














                          first select record



                          var person = db.Person.FirstOrDefault(p => p.ID == inputId);
                          if(person == null) //check if not exist
                          {
                          //add
                          db.Person.Add(PersonDetails);
                          db.SaveChanges();
                          }
                          // else return person object
                          return person;





                          share|improve this answer






























                            0














                            first select record



                            var person = db.Person.FirstOrDefault(p => p.ID == inputId);
                            if(person == null) //check if not exist
                            {
                            //add
                            db.Person.Add(PersonDetails);
                            db.SaveChanges();
                            }
                            // else return person object
                            return person;





                            share|improve this answer




























                              0












                              0








                              0







                              first select record



                              var person = db.Person.FirstOrDefault(p => p.ID == inputId);
                              if(person == null) //check if not exist
                              {
                              //add
                              db.Person.Add(PersonDetails);
                              db.SaveChanges();
                              }
                              // else return person object
                              return person;





                              share|improve this answer















                              first select record



                              var person = db.Person.FirstOrDefault(p => p.ID == inputId);
                              if(person == null) //check if not exist
                              {
                              //add
                              db.Person.Add(PersonDetails);
                              db.SaveChanges();
                              }
                              // else return person object
                              return person;






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 22 '18 at 7:28

























                              answered Nov 22 '18 at 7:23









                              SaifSaif

                              1,0431723




                              1,0431723























                                  -1














                                  You can check existence of record with .Any() method:



                                  if(db.Person.Any(p => p.ID == inputID))
                                  {
                                  return db.Person.First(p => p.ID == inputID);
                                  }
                                  else
                                  {
                                  //Set user notification message
                                  }





                                  share|improve this answer




























                                    -1














                                    You can check existence of record with .Any() method:



                                    if(db.Person.Any(p => p.ID == inputID))
                                    {
                                    return db.Person.First(p => p.ID == inputID);
                                    }
                                    else
                                    {
                                    //Set user notification message
                                    }





                                    share|improve this answer


























                                      -1












                                      -1








                                      -1







                                      You can check existence of record with .Any() method:



                                      if(db.Person.Any(p => p.ID == inputID))
                                      {
                                      return db.Person.First(p => p.ID == inputID);
                                      }
                                      else
                                      {
                                      //Set user notification message
                                      }





                                      share|improve this answer













                                      You can check existence of record with .Any() method:



                                      if(db.Person.Any(p => p.ID == inputID))
                                      {
                                      return db.Person.First(p => p.ID == inputID);
                                      }
                                      else
                                      {
                                      //Set user notification message
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 '18 at 8:23









                                      OleksiiYatsenkoOleksiiYatsenko

                                      30339




                                      30339






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425622%2fc-sharp-if-element-not-found-then-something-else%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          404 Error Contact Form 7 ajax form submitting

                                          How to know if a Active Directory user can login interactively

                                          TypeError: fit_transform() missing 1 required positional argument: 'X'