I have a gridview that is populated from an entitydatasource in asp.net. In my c# code-behind I need to add a WHERE parameter to the entity data source that will filter out all data that is not >= validDate1 and <= validDate2.
Here is where I’m at:
using (RamRideOpsEntities myEntities = new RamRideOpsEntities())
{
var validDates = (from a in myEntities.AdminOptions
select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();
if (validDates != null)
{
RidesGridView.Where = " ..... ??? " //TODO
}
}
Edit: with the answers below, let me be more clear.. the validDates = …. statement is just getting the two valid dates I need to filter by.. there ‘where’ clause needs to be added to the entity data source so the data displayed in the grid view is within the valid date range.
You’re using an EntityDataSource. This is a very un-linq type which allows you to use runtime-defined strings to modify queries. A more linq-ish technique would prefer that the string is part of the language of the program, and the compiler would turn it into an expression tree (as others have posted). That linq-ish approach won’t work as long as you’re using an EntityDataSource.
From the msdn article referencing EntityDataSource.Where, it looks like you need to use the magic word “it” to describe the row. Then you’d supply the parameters to the
.WhereParameterscollection.Code: