Programmatic composition of Linq query at runtime: Syntax to specify datatable? RRS feed

  • Question

  • Does anyone know the syntax for composing a Linq query programmatically at runtime based on a table name in a string variable?

    Something like the following:

         Dim query = from i in <stringTableName> where <stringWhereClause> select i


    Tuesday, May 19, 2009 10:28 PM

All replies

  • I have a pretty similiar problem.  I'd like to be able to change the query to only evaluate the "Where" clause (or the "And") if the variable has a value.  For example if there is no first name only query for last name etc...


    Dim cleanContacts = From Clean_Partner In db.Clean_Partners _
    Where Clean_Partner.LastName.Equals(lastName) _
    And Clean_Partner.FirstName.Equals(FirstName) _
    Return cleanContacts.ToList()


    I realize the quick and easy solution would be to do something like

    If FirstName<>"" then
      Do it this way
      Do it that way
    End If

    But I have 9 different varialbes for which a user can use any combination of to "filter" the dataset.  I'd really prefer not to have to create a select for every possible combination.


    Tuesday, May 19, 2009 11:49 PM
  • Microsoft's dynamic query library should help:


    Write LINQ queries interactively -
    Wednesday, May 20, 2009 4:57 AM