Linq 的链接 - 即使是正确的 google 问题也可以

发布于 2024-10-30 23:26:03 字数 568 浏览 0 评论 0原文

我有一个常见问题,对此的解释可能会太长。对于我需要的信息的良好链接将不胜感激。我已经用谷歌搜索过,但不完全确定我在寻找什么。

我必须在数据库中搜索表单的结果。用户有大约 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 技术交流群。

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

发布评论

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

评论(3

感情旳空白 2024-11-06 23:26:05

PredicateBuilder 允许您组装谓词以在动态查询中使用。

The PredicateBuilder lets you assemble predicates to use in dynamic queries.

带上头具痛哭 2024-11-06 23:26:05

您可以动态构建表达式谓词动态构建表达式树< /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…

蓝天白云 2024-11-06 23:26:05

这并不完全是您所要求的,因为执行相等检查而不是类似检查,但它可能会为您指明正确的方向:

List<String> names = this.namesField.Text.Split(new Char[]() { ' ' }).ToList<String>();
List<String> surnames = this.surnamesField.Text.Split(new Char[]() { ' ' }).ToList<String>();

var query = from a in db.Persons
            where names.Contains(a.Name) && surnames.Contains(a.Surname)
            select a;

应该有一个类似于 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:

List<String> names = this.namesField.Text.Split(new Char[]() { ' ' }).ToList<String>();
List<String> surnames = this.surnamesField.Text.Split(new Char[]() { ' ' }).ToList<String>();

var query = from a in db.Persons
            where names.Contains(a.Name) && surnames.Contains(a.Surname)
            select a;

There should be a method similar to List<T>.Contains() which performs a partial match

Disclaimer: I have no access to Visual Studio right now so I'm writing this code by memory, typos may be included!

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