test

CSharp · Console

/Program.cs

using System;
using System.Linq; 
using System.Collections;
using System.Collections.Generic;		
using System.Data;
using System.Data.Linq;
using System.Data.DataSetExtensions;
using System.Xml;
using System.IO;
using System.Xml.Linq;

public class Program
{
	public static void Main()
	{			
		DataTable table = new DataTable();
		
		table.Columns.Add("userId", typeof(string));
		table.Columns.Add("createdTime", typeof(DateTime));
		
		
		table.Rows.Add(new object[] {"1",  DateTime.Now.Date.AddDays(2)});
		table.Rows.Add(new object[] {"4",  DateTime.Now.Date.AddDays(3)});
		table.Rows.Add(new object[] {"1",  DateTime.Now.Date.AddDays(2)});
		table.Rows.Add(new object[] {"3",  DateTime.Now.Date.AddDays(4)});
		
		var result = table.AsEnumerable().Where(u=> u.Field<DateTime>("createdTime") > DateTime.Now.AddDays(-7)) //subtract a week
			.GroupBy(g=> new { userid = g.Field<string>("userId") , span = g.Field<DateTime>("createdTime").Minute })
			.Select(g=> new { userid = g.Key.userid, count = g.Count()})
			.GroupBy(g=> g.userid ).Select(s=> new {userid = s.Key, count = s.Count()}); 
		
		foreach(var res in result)
		{
			Console.WriteLine("{0} - {1}", res.userid, res.count);
		}
		
	}
}

;