views:

246

answers:

1

I have a listbox that is dynamically filled by certain values depending on what table has been selected from another list box. Once a value is selected, it is being graphed vs a date & time range. With my ignorance of linq: depending on what value is selected, the linq to sql statement I need to create to grab data from my database is different as I can't use an index on an anonymous type.

result = From t In db.APS _
         Where t.DateTime >= startDate And _
         t.DateTime <= finishDate And t.Weight = weight _
         Select t.DateTime, t.TotalConcentration

t.TotalConcentration should be selected if my listbox value is "Total Concentration", but if it's something else, like "Temperature" or "Flow Rate" (connected to appropreate database columns) - this method obviously isn't going to work. I need to be able to dynamically select a specific column from the anonymous type list, or use some other method I'm unaware of to do this. I'm using VB, but if you have a solution in C# it would also be appreciated.

A: 

Have a look at Dynamic Linq. Dynamic Linq allows you to specify field and table names in your Linq statements as strings, so you should be able to pass it a string from your listbox to specify the field you want to Select.

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

Robert Harvey
I managed to get what I want done with this, but it seems useless to use linq, then override half of its functionality.
Geodesic