Dynamic Like clause in LINQ How?

J

john

I'm trying to build a LINQ expression that will use a dynamic construction
of a LIKE statement in the WHERE clause, it would look something like this
in SQL:

WHERE TaskGroup Like "*00*" OR TaskGroup Like "*20*"

It would be many variations on the above.



I know how use the LIKE clause in LINQ but not in this context, here is an
attempt that did not work, I also tried building it into a variable that
looked like the LINQ LIKE clause if I were to type it out (WHERE filterExp)
but it did not work either.



Thanks for any input



filterExp = ""

i = InStr(1, strFilter, ":")

Do Until i = 0

filterExp = filterExp & """Like *" & Mid(strFilter, i - 2, 2) &
"*"""

i = InStr(i + 1, strFilter, ":")

If i <> 0 Then

filterExp = filterExp + ", "

End If

Loop



Dim grp As String() = {filterExp}

Dim dbTask As New DataTaskDataContext

Dim taskTbl = From tas In dbTask.tblTasks _

Where grp.Contains(tas.TaskGroup) _

Select tas
 
S

Steven Cheng[MSFT]

Hi John,

As for LIKE syntax query, I've found some similar threads discussing on
this, and some of them're using a string comparing approach to do the LIKE
query in LINQ:

http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-li
ke-operator.aspx

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1570290&SiteID=1

You may have a look to see whether it helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "john" <[email protected]>
Subject: Dynamic Like clause in LINQ How?
Date: Sat, 2 Feb 2008 07:13:01 -0500

I'm trying to build a LINQ expression that will use a dynamic construction
of a LIKE statement in the WHERE clause, it would look something like this
in SQL:

WHERE TaskGroup Like "*00*" OR TaskGroup Like "*20*"

It would be many variations on the above.



I know how use the LIKE clause in LINQ but not in this context, here is an
attempt that did not work, I also tried building it into a variable that
looked like the LINQ LIKE clause if I were to type it out (WHERE filterExp)
but it did not work either.



Thanks for any input



filterExp = ""

i = InStr(1, strFilter, ":")

Do Until i = 0

filterExp = filterExp & """Like *" & Mid(strFilter, i - 2, 2)
&
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top