views:

294

answers:

5

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.

+1  A: 

Try these links:

http://www.velocityreviews.com/forums/t589023-dynamic-like-clause-in-linq-how.html http://www.simonrhart.com/2008/06/using-like-in-linq-to-sql-under-c.html
http://forums.asp.net/t/1217305.aspx
http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-like-operator.aspx

Albert
I think the SqlMethods.Like is the best solution since it gives real pattern matching.
Payton Byrd
A: 

Contains is .Contains For like check this http://davidhayden.com/blog/dave/archive/2007/11/23/LINQToSQLLIKEOperatorGeneratingLIKESQLServer.aspx

This is from first link in google request "like operator in linq to sql". Please, google first.

Andrey
A: 

Actually, there is direct support for the like operator in Linq2Sql:

db.MyTable.Where(a => SqlMethods.Like(a.Name, "%"+searchTerm+"%"))

See here:

http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods_members.aspx

... but I prefer using startsWith, endsWith and contains for most applications.

spender
+1  A: 

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.

Curtis White
A: 

Please note that the solution provided only works for simple cases: if you want to use parameters for the source of the Contains call, and/or if you want to use the current queried collection to be used as parameter for the Contains call, then the solution provided won't work.

You can find in the following blog post a solution to properly extend the Dynamic Linq library and add support for Contains extension:

http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html

Walter Almeida