具有 Contains 和可为 Null 值的 Linq 查询

发布于 2024-10-30 22:21:54 字数 440 浏览 1 评论 0原文

我有一个搜索方法,如下所示:

public IEnumerable<Result> Search(string searchText)
{

     return _context.Person.Where(x => x.Contains(searchText));
}

我希望能够在 searchText 为空/空的情况下调用此函数并取回所有记录。

我尝试过这个,但没有成功:

return _context.Person.Where(x => x.Contains(searchText ?? ""));

除了将其分为两个步骤并在将其应用于查询之前在 if 语句中检查 searchString 之外,还有其他方法可以实现此目的吗?

I have a method for searching which looks like this:

public IEnumerable<Result> Search(string searchText)
{

     return _context.Person.Where(x => x.Contains(searchText));
}

I want to be able to call this function with searchText being null/empty and get all of the records back.

I have tried this with no luck:

return _context.Person.Where(x => x.Contains(searchText ?? ""));

is there another way to accomplish this besides breaking it up into two steps and checking searchString in an if statement before applying it to the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

听,心雨的声音 2024-11-06 22:21:55
public IEnumerable<Result> Search(string searchText)
{
    if(string.IsNullOrEmpty(searchText))
        return _context.Person;
    else
        return _context.Person.Where(x => x.Contains(searchText));
}
public IEnumerable<Result> Search(string searchText)
{
    if(string.IsNullOrEmpty(searchText))
        return _context.Person;
    else
        return _context.Person.Where(x => x.Contains(searchText));
}
无人问我粥可暖 2024-11-06 22:21:55

你可以这样做:

return _context.Person.Where(x => 
   string.IsNullOrEmpty(searchText) || 
   x.Contains(searchText)
);

这是一个我经常使用的模式,当我有参数时,我只想在设置它们时应用它们。

You can do it like this:

return _context.Person.Where(x => 
   string.IsNullOrEmpty(searchText) || 
   x.Contains(searchText)
);

This is a pattern I use a lot when I have parameters I want to apply only if they are set.

近箐 2024-11-06 22:21:55

还有一种效率较低的方法...不确定这在语义上是否正确,但你明白了...

return _context.Person.Where((x, index) => x.Contains(searchText ?? x[index]));

And a less efficient way... Not sure if this is semantically correct but you get the idea...

return _context.Person.Where((x, index) => x.Contains(searchText ?? x[index]));
风蛊 2024-11-06 22:21:55
return _context.Person.Where(x =>string.IsNullOrEmpty(searchText) ? true : x.Contains(searchText));

或者

public IEnumerable<Result> Search(string searchText)
    {
        return string.IsNullOrEmpty(searchText) ? _context.Person : _context.Person.Where(x => x.Contains(searchText));
    }
return _context.Person.Where(x =>string.IsNullOrEmpty(searchText) ? true : x.Contains(searchText));

or

public IEnumerable<Result> Search(string searchText)
    {
        return string.IsNullOrEmpty(searchText) ? _context.Person : _context.Person.Where(x => x.Contains(searchText));
    }
难忘№最初的完美 2024-11-06 22:21:55

假设 Person 是一个类 Contains 似乎是该类的方法。类似 Where(x => x.Contains(searchText))Where(x => string.IsNullOrEmpty(searchText) || x.Contains(searchText)) 的表达式code> where x is a Person 根本无法与 LINQ to Entities 一起使用,即使是像 ...

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public bool Contains(string searchText)
    {
        return Name.Contains(searchText);
    }
}

... 这样的简单类,它也会抛出异常,因为 LINQ to Entities 无法将此方法转换为存储表达式。 Where(x => string.IsNullOrEmpty(searchText) || x.Name.Contains(searchText)) 可以工作。

Assuming that Person is a class Contains seems to be method of this class. An expression like Where(x => x.Contains(searchText)) or Where(x => string.IsNullOrEmpty(searchText) || x.Contains(searchText)) where x is a Person won't work at all with LINQ to Entities, even with a simple class like ...

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public bool Contains(string searchText)
    {
        return Name.Contains(searchText);
    }
}

... it will throw an exception because LINQ to Entities can't translate this method to a storage expression. Where(x => string.IsNullOrEmpty(searchText) || x.Name.Contains(searchText)) would work though.

·深蓝 2024-11-06 22:21:54
_context.Person.Where(x => string.IsNullOrEmpty(searchText) ? true : x.Contains(searchText));
_context.Person.Where(x => string.IsNullOrEmpty(searchText) ? true : x.Contains(searchText));
帅冕 2024-11-06 22:21:54

我尝试了所有解决方案,下面的解决方案对我有用

query = query.Where(e =>  e.CategoryType.HasValue && categoryTypes.Contains(e.CategoryType.Value));

问题

I try all solutions and just below solution worked for me

query = query.Where(e =>  e.CategoryType.HasValue && categoryTypes.Contains(e.CategoryType.Value));

Issues

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文