7

When I run a linq query I get back the results only when an item can be found; if no items are found, it is throwing an exception "object reference not set to an instance of an object"

How do I not throw this exception? I want only to return a result even if it is empty.

var id = db.table.Where(a => a.item == passed_item).FirstOrDefault().id;
0

4 Answers 4

26

This method, FirstOrDefault, will return null, if an object not been found. Hence, if you try to read the value of id, then an exception will be thrown.

One way to avoid this is the following:

// I suppose that the id you want to read is an int.
// If it isn't, please change the code correspondingly. 
int id;

// Try to get the record.
var record = db.table.Where(a => a.item == passed_item)
                     .FirstOrDefault();

// If you find the record you are looking for, then read it's id.
if(record != null) 
{
    id = record.id;
}

Update

Another option it would be to follow that DavidG suggested in his comment:

var record = db.Table.FirstOrDefault(a => a.item == passed_item);

and the next step is the same.

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

3 Comments

Or potentially db.Table.FirstOrDefault(a => a.item == passed_item)
@DavidG you are correct. I copied and pasted David's code and I didn't pay attention on this. Thanks for your comment.
Or 1 line var id = (db.table.FirstOrDefault(a => a.item == passed_item))?.id;
5

You could use the Null-conditional operator instead:

int? id = db.table.Where(a => a.item == passed_item).FirstOrDefault()?.id;

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-

Comments

2
var id = (from a in db.table
      where a.item == passed_item
      select a.id).FirstOrDefault();

This will ensure that it only tries to dereference id when something is found. Other wise, id will be assigned whatever the default value for the id property is (null or zero)

1 Comment

good answer! This only select the ID field from the database - and are therefor more efficient
1

I saw the cause in null being one of the values.

SImageName = (Users.SImageName != null ? Users.SImageName : "User.png")

Check in your data there is no NULL value

                var userlist = (from Users in _context.TbUsersInfos
                            join Orgs in _context.TbBaseOrgs
                               on Users.NOrgCode equals Orgs.NCode
                            where
                               Users.NOrgCode == 1
                            orderby
                               Users.NUserCode
                            select new
                            {
                                NUserCode = Users.NUserCode.ToString(),
                                SImageName = (Users.SImageName != null ? Users.SImageName : "User.png"),
                            }).ToList();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.