C# Lambda .Contains() 多个属性

发布于 2024-10-16 23:31:44 字数 397 浏览 2 评论 0原文

我们使用以下内容生成搜索查询(使用 NHibernate)。

GetAll(x => x.Username.ToUpper().Contains(SEARCH)).ToList();

是否可以搜索(包含)多个属性,例如...

    GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        && x => x.Firstname.ToUpper().Contains(SEARCH) 
        && x => x.Lastname.ToUpper().Contains(SEARCH)).ToList();

使用 C#

We are using the following to generate a search query (using NHibernate).

GetAll(x => x.Username.ToUpper().Contains(SEARCH)).ToList();

Is it possible to do a search (Contains) for multiple properties, something like ...

    GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        && x => x.Firstname.ToUpper().Contains(SEARCH) 
        && x => x.Lastname.ToUpper().Contains(SEARCH)).ToList();

Using C#

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

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

发布评论

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

评论(2

零度℉ 2024-10-23 23:31:44
GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        && x.Firstname.ToUpper().Contains(SEARCH) 
        && x.Lastname.ToUpper().Contains(SEARCH)).ToList();

我想你会想在 OR 上进行搜索:

GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        || x.Firstname.ToUpper().Contains(SEARCH) 
        || x.Lastname.ToUpper().Contains(SEARCH)).ToList();
GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        && x.Firstname.ToUpper().Contains(SEARCH) 
        && x.Lastname.ToUpper().Contains(SEARCH)).ToList();

I'd imagine you'd want to do a search on OR though:

GetAll(x => x.Username.ToUpper().Contains(SEARCH) 
        || x.Firstname.ToUpper().Contains(SEARCH) 
        || x.Lastname.ToUpper().Contains(SEARCH)).ToList();
仅此而已 2024-10-23 23:31:44

尝试 NinjaNye.SearchExtensions

它将允许使用以下语法:

var result = GetAll().Search("search", 
                             x => x.Username, 
                             x => x.Firstname, 
                             x => x.Lastname)
                     .ToList();

当与 sql 一起使用时,这将产生如下所示的内容:

SELECT [Extent1].[Id] AS [Id], 
       [Extent1].[Username] AS [Username], 
       [Extent1].[Firstname] AS [Firstname], 
       [Extent1].[Lastname] AS [Lastname]
FROM   [dbo].[Users] AS [Extent1]
WHERE ([Extent1].[Username] LIKE N'%search%') 
   OR ([Extent1].[Firstname] LIKE N'%search%') 
   OR ([Extent1].[Lastname] LIKE N'%search%')

... 意味着所有工作都是在数据源上完成的,而不是在内存中

对于源代码,请查看 github 页面:

https://github.com/ninjanye/SearchExtensions

Try NinjaNye.SearchExtensions.

It will allow the following syntax:

var result = GetAll().Search("search", 
                             x => x.Username, 
                             x => x.Firstname, 
                             x => x.Lastname)
                     .ToList();

When used with sql, this will produce something like the following:

SELECT [Extent1].[Id] AS [Id], 
       [Extent1].[Username] AS [Username], 
       [Extent1].[Firstname] AS [Firstname], 
       [Extent1].[Lastname] AS [Lastname]
FROM   [dbo].[Users] AS [Extent1]
WHERE ([Extent1].[Username] LIKE N'%search%') 
   OR ([Extent1].[Firstname] LIKE N'%search%') 
   OR ([Extent1].[Lastname] LIKE N'%search%')

... meaning all the work is done at the data source, and not in memory

For the source code, take a look at the github page:

https://github.com/ninjanye/SearchExtensions

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