I have an object Orders (Order, Product, Cost, Price, Client, Day).
I am retrieving a lot of records from the database and to facilitate filtering, I need to pouplate DropDownLists with the distinct values retrieved so the user can select only specific dates or specific products or even a specific price. The IDs of the DropDownList are created as “ddl_” + name of the concerned field.
What I’d like is to be able to define a simple array like this:
public string[] filterArray = new string[] { "Order", "Product", "Cost", "Price", "Client", "Day" };
Then call a BindDLLs method:
foreach (string filterName in filterArray)
{
// Find the ddl to populate
DropDownList ddl = (DropDownList)this.FindControl("ddl_" + filterName);
// Get the data for that ddl only (use filterName in the select...)
var query = (from items in results select items.filterName.ToString()).Distinct();
// Populate the ddl (not complete code...)
foreach (var item in query)
{
ddl.Items.Add(item...);
}
}
My problem is that I find plenty of documentation on how to modify the WHERE or GROUP BY or any other parts of linq statements at run-time. I just don’t find any on how to change dynamically the field I want to retrieve.
Is there an easy way to do this? Thank you.
Yipi
Edit:
List<Orders> results = OrdersService.GetOrders();
public class Orders
{
[DataMember]
public DateTime? Day
[DataMember]
public int? Order
[DataMember]
public int? Product
[DataMember]
public int? Cost
[DataMember]
public int? Price
[DataMember]
public int? Client
[DataMember]
}
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx