How to use Dynamic LINQ (System.Linq.Dynamic) for LIKE operation?

Can someone tell me how I can use the LIKE statement using System.Linq.Dynamic ?

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

 /* 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 a bunch

+10
c # linq dynamic-linq
source share
7 answers

Just try "CityName.Contains (@ 1)", this will convert to the correct lambda from the moment the method is called on an accessible type.

something like:

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

Just tested it with an example application that comes with a dynamic library, and it generates a LIKE statement

+22
source share

This will allow the LIKE operator in integer fields:

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

Just add more offers.

 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 request will create it:

select * at the client, where CityName and CityName, for example, "London", "USA", etc.

Do you want to

select * at the client, where CityName as "London" or CityName as "USA", etc.

To use Dynamic Created or statements, you can use predicatebuilder , there really is a lot of functionality 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")); 
+3
source share

You can use .StartsWith(), .EndsWith() and .Contains() , which will generate LIKE SQL with trailing, leading, and surrounding wildcards, respectively. I don’t know how to create an instruction with an integrated template.

+3
source share

@Jaime is what I need, thanks.

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

+2
source share

you can try this.

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

I made my own C # as an evaluator, and connected it to a dynamic library. Take a look at my blog post http://blog.alivate.com.au/?p=382

0
source share

Source: https://habr.com/ru/post/650695/


All Articles