Monday, April 13, 2009

Compare DateTime - Linq and orderby

I had to create a small chart about Asp.Net Membership users. The easy way how to do this is to use the Method GetAllUsers(). Once we loaded all Membership data we ready to run over our data with 2 simple LINQ statements:

      DateTime now = DateTime.UtcNow;
Dictionary<datetime,> day = new Dictionary<datetime,>();
for (int i = 0; i < 7; ++i)
{
DateTime compareDate = new DateTime(now.Year, now.Month, now.Day);
compareDate = compareDate.AddDays(-i);
var d = from a in data
where a.CreationDate.Year == compareDate.Year &amp;&amp;
a.CreationDate.Month == compareDate.Month &amp;&amp;
a.CreationDate.Day == compareDate.Day
orderby a.CreationDate descending
select a;
day.Add(compareDate, d.Count());
}

Dictionary<datetime,> month = new Dictionary<datetime,>();
for (int i = 0; i < 12; ++i)
{
DateTime compareDate = new DateTime(now.Year, now.Month, now.Day);
compareDate = compareDate.AddMonths(-i);
var d = from a in data
where a.CreationDate.Year == compareDate.Year &amp;&amp;
a.CreationDate.Month == compareDate.Month
orderby a.CreationDate descending
select a;
month.Add(compareDate, d.Count());
}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge