如何使用LINQ中的where子句处理空值?

时间:2018-04-18 08:01:19

标签: c# linq

我有两个实体,我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();

此搜索逻辑会将值与NamePatientEmail进行比较,如果列表中的prop为null,则返回的结果为空!为什么(||)&#34;或&#34;运算符是不是在跳过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"
}

3 个答案:

答案 0 :(得分:2)

好的,我做了一个小测试,这让我猜对了。 代码段:

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)解决此问题。

正如预期的那样,有一个NRE被抛出。您正在某处处理异常并返回一个空列表,或者只是不使用结果项填充您的列表(结果是相同的)。

答案 1 :(得分:1)

||运算符加String.IsNullOrWhiteSpace将包含null的值。然后,您尝试对ToLower()值进行null

试试这个。首先要确保电子邮件的valie不为null,然后执行contains。

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 ();

答案 2 :(得分:1)

例如:

AliasName = p.AliasName,

替换为

AliasName = p.AliasName ?? ""

当p.AliasName为null时,AliasName为空字符串。