2

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!

9
  • Logic by exception is almost always wrong. Commented Jul 20, 2012 at 13:48
  • 1
    Calling .ToList<User> on the result that returns no matches should work just fine. It will just be a list with 0 entries in it. Commented Jul 20, 2012 at 13:53
  • that was what i first tried. but when i try to add a result with no matches to any sort of list, it throws an exception. so my workaround was to use this thrown exception as a way to execute the rest of my code. Commented Jul 20, 2012 at 13:56
  • When you call temp = userIsNew.ToList<Users>(), what exception is thrown when the result is empty? Commented Jul 20, 2012 at 13:58
  • the exception only says 'an error occurred while processing this request' Commented Jul 20, 2012 at 14:07

2 Answers 2

2
        var userIsNew = (from f in controlEntities.Users
                                where (f.UserId == userIdTextBox.Text)
                                select f).FirstOrDefault();


        if (userIsNew != null)
        {
            MessageBox.Show("This UserId already exists in the Database. \nPlease try another UserId.");
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Being null and empty are two different things so I am not convinced that this is a correct answer. I have seen a query return a non-null object even though the query results within the object indicate nothing was found.
FirstOrDefault should return null if no results are found always surely? Can you give detail on how it didnt with 0 results
0

Another way would be:

        bool userIsNew = controlEntities.Users.
            Count(f => f.UserId == userIdTextBox.Text) == 0;

        if (!userIsNew)
        {
            MessageBox.Show("This UserId already exists in the Database. \nPlease try another UserId.");
        }

It's efficient because the data server returns only a number instead of a resulset.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.