VB Dynamic Linq

4 posts, 0 answers
  1. Mike
    Mike avatar
    31 posts
    Member since:
    Apr 2009

    Posted 09 Apr 2013 Link to this post

    I am attempting to build a dynamic LINQ query to filter users. Here is my code so far:
    'Initialise the contexts
    Dim portal As New Domain.vPortal.DbContext
     
    'Get the basic user search
    Dim search = (From u In portal.Users Select u)
     
    'Apply the filter criteria
    If Not filter.Name Is Nothing Then
        search.Where(Function(e) e.Username = filter.Name)
    End If
     
    'Get the results
    Dim results = search.ToList

    The problem I am having is that the where clause is not being applied to the LINQ query. If I set a username of 'blah' the linq query still returns all users regardless. What am I doing wrong?

    Thanks
  2. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 09 Apr 2013 Link to this post

    Hi Mike,

    You almost got it.
    You have to change the following line to have the behaviour you desire:
    'Apply the filter criteria
    If Not filter.Name Is Nothing Then
        search = search.Where(Function(e) e.Username = filter.Name)
    End If
    See the change in the bold.

    All the best,
    Viktor Zhivkov
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  3. DevCraft banner
  4. Mike
    Mike avatar
    31 posts
    Member since:
    Apr 2009

    Posted 09 Apr 2013 Link to this post

    Doh! I should have seen that. Thanks.
  5. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 09 Apr 2013 Link to this post

    Hello Mike,

    You are welcome.
    If you happen to need any assistance do not hesitate to contact us again.

    Kind regards,
    Viktor Zhivkov
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top