Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "contains" or "like" in a dynamic linq query?

The help file that came with Dynamic Linq in the CSharpSamples.zip does not show any examples of using contains or like.

Are there any simple workarounds for doing this? i.e where (col like @col) doesn't work.

like image 218
Curtis White Avatar asked Mar 16 '10 15:03

Curtis White


People also ask

Can you use LINQ on dynamic?

The Dynamic source file includes a helper library that allows you to express LINQ queries using extension methods that take string arguments instead of type safe operators. To use the Dynamic Expression API, you could simply copy/paste the Dynamic source file in your project.

What is contains in LINQ C#?

LINQ Contains is quantifier operator. In LINQ Contains it also checks with the data sources, to check whether the collection of lists contains their desired element or not, and then it returns the result as either true or false based on the expected outcomes of the result.

What is System LINQ dynamic?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.


2 Answers

Here is the answer! The Dynamic Linq does support the . operator,

According to the docs:

"Instance field or instance property access. Any public field or property can be accessed."

Thus, it is possible to use this syntax

.Where("MyColumn.Contains(@0)", myArray) 

Thanks for all the suggestions! And thanks to me for finding the solution.

like image 186
Curtis White Avatar answered Sep 19 '22 03:09

Curtis White


For me the solution was outerIt.

   class User { public string Name { get; set; } }    ...    IQueryable<User> query = db.Users;    ...    query = query.Where("@0.Contains(outerIt.Name)", list); 

Note that outerIt is kind of a keyword built in the library (you don't need to modify it as you can read it in an answer here). You can access the property of your query's type through it.

like image 24
user2042930 Avatar answered Sep 21 '22 03:09

user2042930