C#/Linq : Filter a list based on From/ To dates (and IENUMERABLE to LIST)

No comments

Filter my List (retur.Tilbudsliste)

based on input of from and to-dates

if (param.FromDato > DateTime.MinValue || param.ToDate > DateTime.MinValue)

{

      //filter on date

      var filtrert =    from t in retur.TilbudsListe

                        where t.DateRegistered >= param.FromDate &&

                             (param.ToDate == DateTime.MinValue || t.DateRegistered <= param.ToDate)

                        select t;

     retur.TilbudsListe = filtrert.ToList();

}

The result filtrert is of type Ienumerable, and is then cast to a list with:

retur.TilbudsListe = filtrert.ToList();

Note that with the check on DateTime.Minvalue we've made both inputs optional,

allowing us to specify none, one or both params.

Posted via email from Henris blogg

No comments :

Post a Comment