none
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

    Thanks,

         -BGood
    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) _
    Select
    Clean_Partner
    Return cleanContacts.ToList()

     



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

    If FirstName<>"" then
      Do it this way
    Else
      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.

    thanks

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

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    Joe

    Write LINQ queries interactively - www.linqpad.net
    Wednesday, May 20, 2009 4:57 AM
    Answerer