Hakan Axheim

Hakan Axheim

  • 1.4k
  • 201
  • 29k

System.Collections.Generic.List<T>.Find(...) returned null.

Oct 23 2020 2:28 AM
Hi,
 
In the source code I am working with  a List containting items from a table in SqlServer.
I wantt to search for a specific item in that list with a unique ID.
When I search for a value that not exist in that List the result will be null and I receive an exception.
 
This is the source code:
 
string resultValue = ItemList.Find(Item => Item.Id.equals(searchId)).Name;
 
If I search for a value (searchId) that doesn't exist in ItemList than an error occurs that the object  System.NullReferenceException: Objectreferens not set ... 

How do I solve this problem?
 
In my version of C# I cannot use Nullable variables. 
 

Answers (2)