高分请教linq的问题
数据表如下:
lid uid ldate
1 1 2013-10-11
2 1 2013-10-12
3 2 2013-10-11
4 2 2013-10-12
5 2 2013-10-13
我想在规定的时间内(如11号-13号)得到人登陆的天数,如下数据
uid Count
1 2
2 3
请教EF里面或linq里面如何分组统计?
[解决办法]
db.Table.GroupBy(x => x.uid).Select(x => new { uid = x.Key, Count = x.Count() });
[解决办法]
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("lid", typeof(int));
dt.Columns.Add("uid", typeof(int));
dt.Columns.Add("ldate", typeof(DateTime));
dt.Rows.Add(1, 1, DateTime.Parse("2013-10-11"));
dt.Rows.Add(2, 1, DateTime.Parse("2013-10-12"));
dt.Rows.Add(3, 2, DateTime.Parse("2013-10-11"));
dt.Rows.Add(4, 2, DateTime.Parse("2013-10-12"));
dt.Rows.Add(5, 2, DateTime.Parse("2013-10-13"));
var result = from p in dt.AsEnumerable()
where p.Field<DateTime>("ldate") >= DateTime.Parse("2013-10-11") && p.Field<DateTime>("ldate") <= DateTime.Parse("2013-10-13")
group p by p.Field<int>("uid") into g
select new { Key = g.Key, Count = g.Count() };
result.ToList().ForEach(x => Console.WriteLine(string.Concat(x.Key, "
[解决办法]
", x.Count)));
}
var query=from t in db.TableName
group t by t.uid into g
orderby g.Key
select new {uid=g.Key,Count=g.Count()};