[英]How to handle null values with where clause in LINQ?
我有兩個實體,我Groupjoin
它們Groupjoin
到一個列表中,並且試圖在不同的屬性中搜索某個值的匹配項。 問題在於,如果一個屬性在列表中具有空值,則搜索將完全忽略其他屬性,並返回空列表。
我正在發布相關代碼,
填充entities
,
List<PatientsRegistrySearchViewModel> SearchList = new List<PatientsRegistrySearchViewModel>();
List<PatientsRegistrySearchViewModel> DataResponse = new List<PatientsRegistrySearchViewModel>();
群組加入
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();
和我的搜索邏輯,
searchResults = (from i in SearchList where (
i.Name.ToLower().Contains (value.ToLower()) ||
i.PatientEmail.ToLower ().Contains (value.ToLower())
) select i).ToList();
該搜索邏輯會將值與Name
或PatientEmail
進行比較,如果列表中任一prop為null,則返回的結果為空! 為什么(||)“或”運算符不跳過null?
清單示例
{
patientFileId: 1111,
Name: "John",
aliasName: null,
patientDob: "1/25/85 12:00:00 AM",
patientAge: 0,
patientEmail: "aamaizar@gmail.com",
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: "aamaizar@gmail.com",
socialSecurityNo: null,
patientMobileNo: "3244990"
}
好的,我做了一個小測試,這使我的猜測確定了。 程式碼片段:
class Program
{
static void Main(string[] args)
{
var searchList = new List<TwoProps>()
{
new TwoProps() {Name = "sdfs1", PatientEmail="mymail@sf.com" },
new TwoProps() {Name = "sdfs2", PatientEmail=null },
new TwoProps() {Name = "sdfs3", PatientEmail="mymail2@sf.com" }
};
var stringToSearch = "myMail".ToLower();
var query = (from listItem in searchList
where listItem.Name.ToLower().Contains(stringToSearch)
|| listItem.PatientEmail.ToLower().Contains(stringToSearch)
select listItem).ToList(); //NullReferenceException is thrown here @ second element
//listItem.PatientEmail.ToLower() => null.ToLower() => NRE
Console.WriteLine(query.Count());
}
private class TwoProps
{
public string Name { get; set; }
public string PatientEmail { get; set; }
}
}
您可以使用...|| (listItem.PatientEmail?.ToLower().Contains(stringToSearch) ?? false)
解決此問題...|| (listItem.PatientEmail?.ToLower().Contains(stringToSearch) ?? false)
...|| (listItem.PatientEmail?.ToLower().Contains(stringToSearch) ?? false)
。
如預期的那樣,將引發NRE。 您正在某處處理該異常並返回一個空列表,或者干脆不用結果項填充列表(結果相同)。
||
運算符加上String.IsNullOrWhiteSpace
將包含為null
。 然后,您嘗試對null
值執行ToLower()
。
嘗試這個。 首先將確保電子郵件的有效期不為空,然后執行包含。
searchResults = (from i in SearchList where (
i.Name.ToLower ().Contains (value.ToLower ()) ||
(!string.IsNullOrWhiteSpace (i.PatientEmail) &&
i.PatientEmail.ToLower ().Contains (value.ToLower ()))
) select i).ToList ();
例如:
AliasName = p.AliasName,
用。。。來代替
AliasName = p.AliasName ?? ""
當p.AliasName為null時,AliasName為空字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.