Using System.Linq.Dynamic in VB.Net

Dynamic Linq

Was trying to using dynamic filter as I would do with datasets. Sometimes you may want to build the query at runtime. Steps are:

  1. Use NuGet to install System.Linq.Dynamic
  2. At top of class Imports System.Linq.Dynamic
  3. Use code as in example below:
Dim queryString1 = "Title = @0"
FilmsList = FilmsList.AsQueryable.Where(queryString1, "Casablanca").ToList

Dim queryString2 = "Title.Contains(@0)"
FilmsList = FilmsList.AsQueryable.Where(queryString2, "a").ToList

Dim queryString3 = "Title = ""Casablanca"""
FilmsList = FilmsList.AsQueryable.Where(queryString3).ToList
FilmDataGrid.ItemsSource = FilmsList

Error 1: Character literal must contain exactly one character

Solution 1: Use double quotes when constructing predicate in code e.g. Dim myFilter = String.Concat( “Title = “””,myTitle,””””)

End.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: