Think about it, can you use
Contains
method on a
null
string? The answer is no.
You have two choices:
1. Check for a null value:
var fldSuccessCount2 = tmp_data
.Where(p => !string.IsNullOrWhiteSpace(p.fld_status_code) &&
p.fld_status_code.Contains("200"))
.Where(p => !string.IsNullOrWhiteSpace(p.fld_output_value) &&
p.fld_output_value.Contains("Success"))
.Distinct()
.Count();
2. use
null coalescing[
^] operator
var fldSuccessCount = tmp_data
.Where(p => (p.fld_status_code?? "").Contains("200"))
.Where(p => (p.fld_output_value?? "").Contains("Success"))
.Distinct()
.Count();
A couple of notes:
1.
Select
is not required for
Count
, only if you need to define a return type that is different to the original type in the list.
2. You do not need to use
List
as
Distinct
takes an
IQueryable
or
IEnumberable
type.