I have two entities where I Groupjoin them into a list and I am trying to search for a match for a certain value in different properties.
The problem is that if one property has null value in the list, the search completly ignores the other properties and returns empty list.
I am posting the relevant code,
populating entities,
List<PatientsRegistrySearchViewModel> SearchList = new List<PatientsRegistrySearchViewModel>();
List<PatientsRegistrySearchViewModel> DataResponse = new List<PatientsRegistrySearchViewModel>();
Groupjoin,
SearchList = (
from p in registryList
join c in registryAccountsList on p.PatientFileId equals c.PatientFileId into g
from c in g.DefaultIfEmpty()
select new PatientsRegistrySearchViewModel {
PatientFileId = p.PatientFileId,
Name = p.FirstName,
AliasName = p.AliasName,
PatientDob = p.PatientDob.ToString(),
PatientAge = 0,
PatientEmail = c?.Email,
SocialSecurityNo = p.SocialSecurityNo,
PatientMobileNo = c?.MobileNo
}).ToList();
and my search logic,
searchResults = (from i in SearchList where (
i.Name.ToLower().Contains (value.ToLower()) ||
i.PatientEmail.ToLower ().Contains (value.ToLower())
) select i).ToList();
this search logic will compare the value to Name or PatientEmail, if either prop is null in the list, the returned result is empty! Why (||) "or" operator is not skipping null?
list example,
{
patientFileId: 1111,
Name: "John",
aliasName: null,
patientDob: "1/25/85 12:00:00 AM",
patientAge: 0,
patientEmail: "[email protected]",
socialSecurityNo: "1212121SSN",
patientMobileNo: "3244990"
},
{
patientFileId: 2222,
Name: "Nicole",
aliasName: null,
patientDob: "1/1/01 12:00:00 AM",
patientAge: 0,
patientEmail: null,
socialSecurityNo: null,
patientMobileNo: null
},
{
patientFileId: 3333,
Name: "Nancy",
aliasName: null,
patientDob: "3/25/85 12:00:00 AM",
patientAge: 0,
patientEmail: "[email protected]",
socialSecurityNo: null,
patientMobileNo: "3244990"
}
null,prop.ToLower ().Contains (value.ToLower ()will simply generate NRE. So why don't you present your real case?LINQquestion, my apologies.