在数据库中搜索与整个参数或参数的任何部分匹配的名称

发布于 2024-11-07 03:20:27 字数 917 浏览 0 评论 0原文

欢迎使用 C# 或 VB.NET。

我正在编写一个查询来选择具有任何参数部分的所有记录。

我有一张名为“员工”的表。有些人的名字是这样的: John David Clark

如果参数是

  • “John”
  • “John David”
  • “David John”
  • “John Clark”
  • “Clark John”

只要参数中有匹配,我就应该能够得到结果。

如果我使用 Function Contains (q.FirstName & " " & q.LastName).Contains(employeeName),如果 employeeName 是“John Clark”,我将不会得到任何结果 功能包含仅查找从左到右的下一个单词。它一次不能匹配一个单词。

这就是我在 Linq to SQL 中使用它的原因:

        Dim employeeName As String
        query = From q In db.Employees _
                Where (q.FirstName & " " & q.LastName).Split(CChar(" ")).Intersect(employeeName.Split(CChar(" "))).Any _
                Select q

我收到以下错误:

本地序列不能在除 Contains() 运算符之外的查询运算符的 LINQ to SQL 实现中使用

是否有另一种方法可以查询具有参数任何部分的 FirstName 和 LastName?

C# or VB.NET are welcome.

I'm writing a query to select all records that has any part of parameter.

I have one table called Employees. Some people have name like this: John David Clark

If the parameter is

  • "John"
  • "John David"
  • "David John"
  • "John Clark"
  • "Clark John"

I should be able to get result back as long as there's a match in the parameters.

If I use Function Contains (q.FirstName & " " & q.LastName).Contains(employeeName), I will not get any result back if employeeName is "John Clark"
Function Contains looks only for next words from left to right. It doesn't match a single word at a time.

So that's why I used this in the Linq to SQL:

        Dim employeeName As String
        query = From q In db.Employees _
                Where (q.FirstName & " " & q.LastName).Split(CChar(" ")).Intersect(employeeName.Split(CChar(" "))).Any _
                Select q

I got the following error:

Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator

Is there another way that I can query for FirstName and LastName that has any part of parameter ?

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

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

发布评论

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

评论(1

莫多说 2024-11-14 03:20:27

这将找到那些名字和姓氏都与搜索字符串匹配的员工:

        var searchText = "John David Clark";
        var tokens = searchText.Split(null);

        var result = db.Employees
            .Where(arg => tokens.Contains(arg.FirstName) && tokens.Contains(arg.LastName))
            .Select(arg => new { arg.LastName, arg.FirstName })
            .ToList();

这将找到那些名字和姓氏都与搜索字符串中的任何名称匹配的员工:

        var searchText = "John David Clark";
        var tokens = searchText.Split(null);

        var result = db.Employees
            .Where(arg => tokens.Contains(arg.FirstName) || tokens.Contains(arg.LastName))
            .Select(arg => new { arg.LastName, arg.FirstName })
            .ToList();

This will find those employees that have both first name and last name match to the search string:

        var searchText = "John David Clark";
        var tokens = searchText.Split(null);

        var result = db.Employees
            .Where(arg => tokens.Contains(arg.FirstName) && tokens.Contains(arg.LastName))
            .Select(arg => new { arg.LastName, arg.FirstName })
            .ToList();

This will find those employees that have last or first name match to any name in the search string:

        var searchText = "John David Clark";
        var tokens = searchText.Split(null);

        var result = db.Employees
            .Where(arg => tokens.Contains(arg.FirstName) || tokens.Contains(arg.LastName))
            .Select(arg => new { arg.LastName, arg.FirstName })
            .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文