4

I retrieve data from database with lambda like

 var obj = DBContext.MyTable.Where(x => x.ID == 2).SingleOrDefault().MyColumn;

Actually , in MyTable , there is no ID with 2 .
So I got this message .

Object reference not set to an instance of an object.

How can I validate it properly ?

0

5 Answers 5

13

Just capture result of query into separate variable and check if any item found before accessing its properties:

var yourItem = DBContext.MyTable.Where(x => x.ID == 2).SingleOrDefault();
if (yourItem != null)
    obj = yourItem.MyColumn;

BTW you can pass predicate into SingleOrDefault method:

var yourItem = DBContext.MyTable.SingleOrDefault(x => x.ID == 2);

Also you can select your property before applying SingleOrDefault

var obj = DBContext.MyTable.Where(x => x.ID == 2)
                           .Select(x => x.MyColumn)
                           .SingleOrDefault();
Sign up to request clarification or add additional context in comments.

1 Comment

@zey I've added one more option - if you need only one property from your object, then you can project results on server side
3

Another way to do this is using the "null-coalescing" ?? operator, which will use the second argument if the first argument is null.

var obj = (DBContext.MyTable.FirstOrDefault(x => x.ID == 2) ?? new MyTable()).MyColumn;

3 Comments

Using DefaultIfEmpty(defaultValue) (as @Microtechie suggested) is a far more elegant (and easy to read) way of doing the same thing. There are places where ?? is very useful, but this isn't really one of them.
Hi @MosheKatz I tried DefaultIfEmpty(defaultValue) before trying my answer and it did not work?
@user2893091 What did you put in as the defaultValue? In your example, it should be DefaultIfEmpty(new MyTable()).
2

You have to check first if returned value is null and then access it:

var temp = DBContext.MyTable.Where(x => x.ID == 2).SingleOrDefault();

if (temp != null)
{
    var obj = temp.MyColumn;
}

Not the most elegant way but I do not know any other.

Comments

1

I prefer DefaultIfEmpty() instead of .FirstOrDefault()

and by doing so we can avoid if check

for example

var obj = DBContext.MyTable.Where(x => x.ID == 2).DefaultIfEmpty().MyColumn;

or

var obj = DBContext.MyTable.Where(x => x.ID == 2).DefaultIfEmpty(string.Empty).MyColumn;

The concept of DefaultIfEmpty is simple: it replaces an empty collection with a collection of one default value.

Default value of int is 0. Thus, DefaultIfEmpty on a List yields a List with one zero element.

Hope it helps.

1 Comment

Note that string.Empty as the "default value" object isn't going to have a MyColumn attribute. The default value in this case needs to be an object of the type represented in the MyTable table.
0
var yourItem = DBContext.MyTable.Where(x => x.ID == 2).ToArray()
if(    yourItem.Length > 0)
   //do stuff

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.