1

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.

6
  • 1
    wouldn't it be better to filter at the database... Feb 5, 2012 at 2:45
  • 1
    I could be wrong, but it was my understanding that when using an entity data source, the where clause would be applied before data was actually retrieved from the db, so in effect that is what I am trying to do.
    – SHeinema
    Feb 5, 2012 at 2:47
  • @SHeinema - No, just try the code we posted. It will work. Feb 5, 2012 at 3:02
  • The validDates object will never be null, so there's no need for the null check.
    – phoog
    Feb 5, 2012 at 3:10
  • @SHeinema - are you using asp.net or winforms? They're different GridView controls. Neither has a Where property though, so I don't know where you're getting that. Feb 5, 2012 at 3:11

3 Answers 3

3

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 .WhereParameters collection.

Code:

using (myEntities = new RamRideOpsEntities())
{
      var validDates = (from a in myEntities.AdminOptions
                        select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();

      if (validDates != null)
      {
         RidesEDS.Where = @"it.TimeOfCall >= @ValidDate1 AND it.TimeOfCall <= @ValidDate2";
         RidesEDS.WhereParameters.Add(@"ValidDate1", DbType.DateTime, validDates.ValidDate1.ToString());
         RidesEDS.WhereParameters.Add(@"ValidDate2", DbType.DateTime, validDates.ValidDate2.ToString());
      }
 }
1
  • Alright Mr David B, I'd say the green check-mark has to go to you. This is what I was trying to get at.. Pretty good guess at the code too, I added an edit with what ended up working. Thanks!
    – SHeinema
    Feb 5, 2012 at 7:55
0
var validDates = (from a in myEntities.AdminOptions where 
    a.ValidDate1 >= validDate1 && a.ValidDate2 <= validDate2
    select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();
0
0

You don't filter the GridView, you filter the data source. The answer will depend on what kind of data source you have, but assuming it's Linq-compatible:

var items = dataSource.Where(
        d => d.Date >= validDates.ValidDate1 && d.Date <= validDates.ValidDate2
    );
RidesGridView.DataSource = items;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.