Linq 的链接 - 即使是正确的 google 问题也可以
我有一个常见问题,对此的解释可能会太长。对于我需要的信息的良好链接将不胜感激。我已经用谷歌搜索过,但不完全确定我在寻找什么。
我必须在数据库中搜索表单的结果。用户有大约 20 个选项可供选择,其中大约 10 个是文本字段。我想让他们能够在文本的开头和结尾使用通配符 * 字符。此外,如果有多个术语,我会将空格解释为||。
虽然为单个属性编写方法很简单,但将其推广到多个属性却很困难。我想做的是创建一个私有方法,它看起来像这样
func<string, bool> getClause(string val, Type property) { }
所以如果用户搜索名字为 bob 、姓氏为 smith 的人,我想获取查询的每个部分,
qry = qry.Where(
getClause(FirstName, typeof(People.First)) &&
getClause(LastName, typeof(People.Last)) &&
... );
我希望这是清除。 谢谢!
I have a common problem for which an explanation would probably be too lengthy. A good link for the information I need would be greatly appreciated. I have googled but am not completely sure what I am looking for.
I have to search a database on the results of a form. The user has about 20 options to select from, about 10 of which are text fields. I want to provide them the ability to use a wildcard * character at the beginning and end of the text. Further, if there are multiple terms I will interpret the space as a ||.
While writing a method for a single property is simple, generalizing it to many is difficult. What I would like to do is create a private method which would look something like
func<string, bool> getClause(string val, Type property) { }
So if a user searches for someone with a FirstName of bob and a LastName of smith, I'd like to get each part of the query with
qry = qry.Where(
getClause(FirstName, typeof(People.First)) &&
getClause(LastName, typeof(People.Last)) &&
... );
I hope this is clear.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PredicateBuilder 允许您组装谓词以在动态查询中使用。
The PredicateBuilder lets you assemble predicates to use in dynamic queries.
您可以动态构建表达式谓词或动态构建表达式树< /a>,甚至还有一个叫做 动态 linq 我也使用过。它看起来像 动态搜索使用 linq 尝试解决与您类似的问题......
You can dynamically build expression predicates or dynamically build expression trees, there is even a thing called dynamic linq which I have also used. It looks like dynamic searching using linq tries to resolve a similar problem as yours…
这并不完全是您所要求的,因为执行相等检查而不是类似检查,但它可能会为您指明正确的方向:
应该有一个类似于
List.Contains()
的方法它执行部分匹配免责声明:我现在无法访问 Visual Studio,因此我凭记忆编写此代码,可能包含拼写错误!
It's not exactly what you asked for, since performs an equality check and not a like, but it might point you into the right direction:
There should be a method similar to
List<T>.Contains()
which performs a partial matchDisclaimer: I have no access to Visual Studio right now so I'm writing this code by memory, typos may be included!