This is a migrated thread and some comments may be shown as answers.

VB Dynamic Linq

3 Answers 40 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 09 Apr 2013, 10:51 AM
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

3 Answers, 1 is accepted

Sort by
0
Viktor Zhivkov
Telerik team
answered on 09 Apr 2013, 11:02 AM
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 >>
0
Mike
Top achievements
Rank 1
answered on 09 Apr 2013, 12:08 PM
Doh! I should have seen that. Thanks.
0
Viktor Zhivkov
Telerik team
answered on 09 Apr 2013, 01:42 PM
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 >>
Tags
LINQ (LINQ specific questions)
Asked by
Mike
Top achievements
Rank 1
Answers by
Viktor Zhivkov
Telerik team
Mike
Top achievements
Rank 1
Share this question
or