0

I'm trying to get my image from my local database in wp8. Some cells in the image column contain the actual image(in a binary format) whereas others are NULL. Here is the code:

             var image_professor = (from n in College.professor
                                  where n.Surname == Getsurname(MyLongListSelector2.SelectedItem.ToString()) && n.FirstName == GetFirstname(MyLongListSelector2.SelectedItem.ToString())
                                  select n.Pic).Single();

Everything works fine if I click on a professor which images is stored in the database,but if a professor without an image is selected then I get a NullException. Is there a way that I could use a if-else check inside my Linq query and somehow get another value if the query run's on Null or should I use another approach.

0

1 Answer 1

1
         var image_professor = (from n in College.professor
                              where n.Surname == Getsurname(MyLongListSelector2.SelectedItem.ToString()) && n.FirstName == GetFirstname(MyLongListSelector2.SelectedItem.ToString())
                              select n.Pic == null ? defaultPic : n.Pic).Single();

Where 'defaultPic' would be a string pointing to a default or no image pic.

You could also try String.IsNullOrEmpty to check if the Pic is null or empty, but i am not sure if it would work in Linq2SQL. I know it would in EF 6.x.

Sign up to request clarification or add additional context in comments.

1 Comment

This entirely solves my problem. I didn't want to fill my database with a default image. I was thinking how I could point to one in a folder. tnx for the if-else(ternary) demonstration!

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.