0

I want to return the category Name or just null within the projection below, but I get a null reference exception, which makes sense if result.CategoryId is null.

return results.Select(i =>
new Model
{
  Score = i.Score
 ,Date = i.InstanceResult.TestDate
 ,Category = categories.Where(c=>c.Id.Equals(i.result.CategoryId)).SingleOrDefault().Name
});
3
  • what's the type of categoryId Commented Apr 7, 2014 at 12:44
  • so don't use c.Id.Equals(...) use c.Id == ... Commented Apr 7, 2014 at 12:45
  • categoryId is int on in the categories list, but CategoryId is a nullable int in the result object. Commented Apr 7, 2014 at 12:51

4 Answers 4

1

You need to check if there's a category with that Id then decide what value to assign.

return results.Select(i =>
new Model
{
     Score = i.Score
     ,Date = i.InstanceResult.TestDate
     ,Category = categories.Any(c => c.Id.Equals(i.result.CategoryId)) ?
                 categories.First(c => c.Id.Equals(i.result.CategoryId)).Name : 
                 null
});

You could tidy this up with an extension method like so

...
Category = categories.GetNameFromId(i.result.CategoryId)
...

public static string GetNameFromId(this IEnumerable<Category> categories, string id)
{
    return categories.Any(c => c.Id.Equals(id)) ?
                 categories.First(c => c.Id.Equals(id)).Name : 
                 null
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use FirstOrDefault like this but you need to execute same query twice, if you want to do that in one statement:

Category = categories
           .FirstOrDefault(c=> c.Id.Equals(i.result.CategoryId)) != null ? 
           categories
           .FirstOrDefault(c=> c.Id.Equals(i.result.CategoryId)).Name : null;

1 Comment

FirstOrDefault returns default(T) if it can't find a matching element, which means you can do Categories != null ? Category.Name : null right away if it's a reference type.
0
return results.Select(i =>
                {
                    var singleOrDefault = categories.SingleOrDefault(c => c.Id.Equals(i.result.CategoryId));
                    return new Model
                        {
                            Score = i.Score,
                            Date = i.InstanceResult.TestDate,
                            Category = singleOrDefault != null ? singleOrDefault.Name : null
                        };
                });

Comments

-1
categories.Where(c=>c.Id.Equals(i.result.CategoryId)).SingleOrDefault()

returns null if nothing has been found. If you then try to access ".Name" it will throw an exception like you have got.

Try to make it that way:

Category singleCategory = categories.Where(c=>c.Id == i.result.CategoryId).SingleOrDefault();
if (singleCategory != null)
{
    // Do something with the Properties
}

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.