i'm using a query to see if a user already exists in the database. if it finds a user, it adds it to the list (not database), and presents a message. if the user does NOT already exist, the program moves on to add the user.
the problem exists when adding the query result to a list, when the result found nothing. if the query found nothing (user does not yet exist) the returned value isn't null or 0, so i'm not sure how to check for this.
my code works fine, but my problem is trying to find a more elegant approach. i try to add the query results to a list. if it his the "catch", it means the user does not exist and it shall be added. right now my code is:
var userIsNew =
from f in controlEntities.Users
where (f.UserId == userIdTextBox.Text)
select f;
List<Users> temp = new List<Users>();
try
{
temp = userIsNew.ToList<Users>();
}
catch
{
//do nothing
}
if (temp.Count > 0)
{
MessageBox.Show("This UserId already exists in the Database. \nPlease try another UserId.");
}
thank you for your help!
.ToList<User>on the result that returns no matches should work just fine. It will just be a list with 0 entries in it.temp = userIsNew.ToList<Users>(), what exception is thrown when the result is empty?