Dynamic Like clause in LINQ How?

Discussion in 'ASP .Net' started by john, Feb 2, 2008.

  1. john

    john Guest

    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
     
    john, Feb 2, 2008
    #1
    1. Advertising

  2. 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" <>
    >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)

    &
    >"*"""
    >
    > 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
    >
    >
    >
    >
     
    Steven Cheng[MSFT], Feb 4, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Vecchi
    Replies:
    1
    Views:
    689
    Ron Vecchi
    Jul 27, 2003
  2. Ron Vecchi
    Replies:
    0
    Views:
    393
    Ron Vecchi
    Jul 30, 2003
  3. Michael C

    SQL LIKE Clause in Access

    Michael C, Jan 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    13,144
    Michael C#
    Jan 18, 2005
  4. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    370
    Mike Gleason jr Couturier
    Nov 5, 2008
  5. EdisonCPP

    Linq where clause

    EdisonCPP, Nov 24, 2008, in forum: ASP .Net
    Replies:
    8
    Views:
    18,878
    bruce barker
    Nov 24, 2008
Loading...

Share This Page