C# if element not found, then something else
I have a simple table
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
add a comment |
I have a simple table
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
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 theif/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
add a comment |
I have a simple table
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
I have a simple table
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
c# linq
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 theif/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
add a comment |
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 theif/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
add a comment |
4 Answers
4
active
oldest
votes
The null-coalescing operator is one way
var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();
add a comment |
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)
{
//...
}
add a comment |
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;
add a comment |
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
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
The null-coalescing operator is one way
var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();
add a comment |
The null-coalescing operator is one way
var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();
add a comment |
The null-coalescing operator is one way
var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();
The null-coalescing operator is one way
var person = db.Person.FirstOrDefault(p => p.ID == inputId) ?? new Person();
edited Nov 22 '18 at 7:32
Jannik
1,05621844
1,05621844
answered Nov 22 '18 at 7:18
fubofubo
29.6k966104
29.6k966104
add a comment |
add a comment |
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)
{
//...
}
add a comment |
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)
{
//...
}
add a comment |
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)
{
//...
}
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)
{
//...
}
answered Nov 22 '18 at 7:39
Stanislav MolchanovskyStanislav Molchanovsky
467116
467116
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
edited Nov 22 '18 at 7:28
answered Nov 22 '18 at 7:23
SaifSaif
1,0431723
1,0431723
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 22 '18 at 8:23
OleksiiYatsenkoOleksiiYatsenko
30339
30339
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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