When filtering a list using lambda where and contains, I get a NullReference error
var servers = _context.Servers.ToList();
servers = servers.Where(t => t.Technology.Contains(technology)).ToList();
But I get a this error:
Exception thrown: 'System.NullReferenceException' in ServerBuildApp.dll ServerBuildApp.Models.Servers.Technology.get returned null.
The list of servers contains a 'Technology' property and it contains the string I am passing it, for example "BIZ"
Any ideas? Or am I doing this completely wrong?
答案 0 :(得分:3)
You need to check the t.Technology for null.
var servers = _context.Servers.ToList();
servers = servers.Where(t => t.Technology?.Contains(technology) is true).ToList();
You should check t.Technology for null and then check it contains.