LINQ To SQL Dynamic Select
- by mcass20
Can someone show me how to indicate which columns I would like returned at run-time from a LINQ To SQL statement?
I am allowing the user to select items in a checkboxlist representing the columns they would like displayed in a gridview that is bound to the results of a L2S query.
I am able to dynamically generate the WHERE clause but am unable to do the same with the SELECT piece. Here is a sample:
var query = from log in context.Logs select log;
query = query.Where(Log => Log.Timestamp > CustomReport.ReportDateStart);
query = query.Where(Log => Log.Timestamp < CustomReport.ReportDateEnd);
query = query.Where(Log => Log.ProcessName == CustomReport.ProcessName);
foreach (Pair filter in CustomReport.ExtColsToFilter)
{
sExtFilters = "<key>" + filter.First + "</key><value>" + filter.Second + "</value>";
query = query.Where(Log => Log.FormattedMessage.Contains(sExtFilters));
}