如何使用动态LINQ(System.Linq.Dynamic)进行LIKE操作?

发布于 2024-10-10 05:55:20 字数 411 浏览 5 评论 0原文

谁能告诉我如何使用 System.Linq.Dynamic 使用 LIKE 运算符?

我需要在动态 where 查询 中添加多个 LIKE 表达式,

/*
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1", "London", "USA")
*/
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1%", "London", "USA")

谢谢堆

Can any body tell me how can I use a LIKE operator using System.Linq.Dynamic?

I need to add more than one LIKE expression in my dynamic where query

/*
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1", "London", "USA")
*/
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1%", "London", "USA")

thanks heaps

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

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

发布评论

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

评论(5

南薇 2024-10-17 05:55:20

尝试简单地使用“CityName.Contains(@1)”,这将转换为正确的 lambda,因为它是对可访问类型的方法调用。

类似于:

var query =
db.Customers.
Where("CityName.Contains(@0) or CityName.Contains(@1)", "London", "USA")

刚刚使用动态库附带的示例应用程序对其进行了测试,它生成了 LIKE 运算符

Try using simply "CityName.Contains(@1)" this will convert to the proper lambda since its a method invocation on an accessible type.

something like:

var query =
db.Customers.
Where("CityName.Contains(@0) or CityName.Contains(@1)", "London", "USA")

Just tested it with the sample app that comes with the dynamic library and it generates the LIKE operator

暖伴 2024-10-17 05:55:20

您可以使用 .StartsWith()、 .EndsWith().Contains() 这将生成带有尾随、前导和周围通配符的 LIKE SQL分别。不知道如何生成带有嵌入通配符的语句。

You can use .StartsWith(), .EndsWith() and .Contains() which will generate LIKE SQL with trailing, leading and surrounding wildcards respectively. Don't know of a way to generate a statement with an embedded wildcard tho.

梦旅人picnic 2024-10-17 05:55:20

这将允许在整数字段上使用 LIKE 运算符:

.Where(searchField + ".ToString().Contains(@0)", searchString);

This will allow the LIKE operator on integer fields:

.Where(searchField + ".ToString().Contains(@0)", searchString);
感情废物 2024-10-17 05:55:20

只需添加更多 where 子句

var query = db.Customers.Where(c=>c.CityName.contains("London"));
query = query.Where(c=>c.CityName.contains("USA"));
query = query.Where(c=>c.CityName.contains("Johannesburg"));

,但上面的查询将创建它:

从客户中选择 *,其中 CityName
像“伦敦”城市名称一样
“美国”等...

你想要的

从客户中选择 *,其中 CityName
例如“伦敦”城市名称
“美国”等...

要使用 Dynamic Created 或语句,您可以使用 predicatebuilder 确实有很多
您可以使用的功能...

http://www.albahari.com/nutshell/predicatebuilder .aspx

var predicate = PredicateBuilder.False<Customer>();
predicate = predicate.Or(c=>c.CityName.Contains("London"));
predicate = predicate.Or(c=>c.CityName.Contains("USA"));
predicate = predicate.Or(c=>c.CityName.Contains("Johannesburg"));

Just add more where clauses

var query = db.Customers.Where(c=>c.CityName.contains("London"));
query = query.Where(c=>c.CityName.contains("USA"));
query = query.Where(c=>c.CityName.contains("Johannesburg"));

but the above query will create it :

select * from Customer where CityName
like "london" and CityName like
"USA" etc...

you want

select * from Customer where CityName
like "london" or CityName like
"USA" etc...

To use Dynamic Created or statements you can use predicatebuilder there's really alot
of functionality there that you can use...

http://www.albahari.com/nutshell/predicatebuilder.aspx

var predicate = PredicateBuilder.False<Customer>();
predicate = predicate.Or(c=>c.CityName.Contains("London"));
predicate = predicate.Or(c=>c.CityName.Contains("USA"));
predicate = predicate.Or(c=>c.CityName.Contains("Johannesburg"));
π浅易 2024-10-17 05:55:20

你可以试试这个。

IList<string> keyword = new List<string>() { "London", "USA", "Johannesburg" };
            var query = db.Customers.Where(c => keyword.Contains(c.CityName));

you can try this.

IList<string> keyword = new List<string>() { "London", "USA", "Johannesburg" };
            var query = db.Customers.Where(c => keyword.Contains(c.CityName));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文