I have been trying to optimize the filtering process of a Collection<Alert>
based on the DateTime GeneratedOn
property of Alert
class.
Below is the code block which filters the List
by taking in From Date
and To Date
.
if (this.Alerts != null)
{
var fromDt = Convert.ToDateTime(this.FromDateAlert.ToShortDateString() + " " + this.FromTimeAlert.ToLongTimeString());
var toDt = Convert.ToDateTime(this.ToDateAlert.ToShortDateString() + " " + this.ToTimeAlert.ToLongTimeString());
if (fromDt > toDt)
{
Messages.InfoMessage("FromDate cannot be greater than ToDate", "Alert"); return;
}
this.IsBusy = true;
var filteredAlerts = this.Alerts.Where(a => (DateTime.Parse(a.Created) >= fromDt) && (DateTime.Parse(a.Created) <= toDt));
this.PagedAlerts = new PagedCollectionView(filteredAlerts);
this.IsBusy = false;
}
Is there a better way of doing this?
DateTime
s directly. – svick Aug 21 '12 at 17:49