简体   繁体   English

使用LINQ实现搜索过滤器时出现问题

[英]Problems implementing a search filter using LINQ

I'm trying to implement a search filter using LINQ in my ASP.Net application. 我正在尝试在ASP.Net应用程序中使用LINQ实现搜索过滤器。

However I'm running into problems. 但是我遇到了问题。 Here is my code: 这是我的代码:

var result = (from x in db.ContactSet
    select new Models.Contact
    {
        AccountId = x.AccountId,
        FirstName = x.FirstName,
        LastName = x.LastName,
        FullName = x.FullName,
        JobTitle = x.JobTitle,
        ParentCustomerId = x.ParentCustomerId,
        EMailAddress1 = x.EMailAddress1,
        Telephone1 = x.Telephone1,
        MobilePhone = x.MobilePhone,
        Fax = x.Fax,
        GenderCode = x.GenderCode,
        BirthDate = x.BirthDate
    }); //? Gets all rows

result = result
    .Where(x =>
        x.FirstName.Contains(model.searchPhrase) ||
        x.LastName.Contains(model.searchPhrase)); //? Search Filter

The Gets all rows code works perfectly in getting all the rows from my database. Gets all rows代码可以完美地从数据库中获取所有行。 However I'm having issues with the Search Filter part of the code. 但是我在代码的Search Filter部分遇到了问题。

model.searchPhrase is a null-able string. model.searchPhrase是可为空的字符串。

Every time I run through the Search Filter nothing gets returned (doesn't matter what the input is. 每次我通过“ Search Filter运行时,都不会返回任何内容(无论输入是什么。

After debugging I noticed that after that block of code runs I get the following Exception Error : 调试后,我注意到该代码块运行后,出现以下Exception Error

Static members: 'NotSupportedException' is a type, which is not valid in the given context. 静态成员:'NotSupportedException'是一种类型,在给定的上下文中无效。 Non-public members: 'new System.Linq.SystemCore_EnumerableDebugView(result).items' thew an exception of type 'System.NotSupportedException' 非公共成员:'new System.Linq.SystemCore_EnumerableDebugView(result).items'这是类型'System.NotSupportedException'的异常

Any idea what's wrong here? 知道这里有什么问题吗?

I've written a library to help with this problem and make it easier to build conditional queries for filtering. 我已经编写了一个库来帮助解决此问题,并使构建条件查询进行过滤变得更加容易。 You can get the package from NuGet here: https://www.nuget.org/packages/LinqConditionalExtensions 您可以从NuGet此处获取软件包: https//www.nuget.org/packages/LinqConditionalExtensions

Your code could be rewritten as: 您的代码可以重写为:

var hasFilter = !string.IsNullOrWhiteSpace(model.searchPhrase);

var result = db.ContactSet
    .WhereIf(hasFilter, x => x.FirstName.Contains(model.searchPhrase) || x.LastName.Contains(model.searchPhrase))
    .Select(x => new Models.Contact
    {
        AccountId = x.AccountId,
        FirstName = x.FirstName,
        LastName = x.LastName,
        FullName = x.FullName,
        JobTitle = x.JobTitle,
        ParentCustomerId = x.ParentCustomerId,
        EMailAddress1 = x.EMailAddress1,
        Telephone1 = x.Telephone1,
        MobilePhone = x.MobilePhone,
        Fax = x.Fax,
        GenderCode = x.GenderCode,
        BirthDate = x.BirthDate
    });

The WhereIf() method will only apply the filter if the condition is true. WhereIf()方法仅在条件为真时才应用过滤器。 In this case, if your model's search-phrase is not null or white-space. 在这种情况下,如果模型的搜索短语不是null或空格。

You can read more about it here: https://github.com/xKloc/LinqConditionalExtensions 您可以在此处了解更多信息: https : //github.com/xKloc/LinqConditionalExtensions

model.searchPhrase is a null-able string as he written. model.searchPhrase是他编写的可为空的字符串。 You cannot compare data type null-able variable with data type (string variable) in LINQ Query. 您不能在LINQ Query中将数据类型的可为空的变量与数据类型(字符串变量)进行比较。 So, it is throwing exception. 因此,它引发异常。

Use following code. 使用以下代码。 It worked for me 对我有用

string SearchValue="";
try
{SearchValue=model.searchPhrase.Tostring();}catch{}

result = result.Where(x => x.FirstName.Contains(SearchValue) ||
        x.LastName.Contains(SearchValue)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM