在数据库中搜索与整个参数或参数的任何部分匹配的名称
欢迎使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将找到那些名字和姓氏都与搜索字符串匹配的员工:
这将找到那些名字和姓氏都与搜索字符串中的任何名称匹配的员工:
This will find those employees that have both first name and last name match to the search string:
This will find those employees that have last or first name match to any name in the search string: