数据库中有一张表:
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](150) NOT NULL,
[date_of_birth] [date] NOT NULL,
我需要为 GridView 获取一个包含 2 列的数据源。
----------------
| age || count |
----------------
| 20 || 3 |
| 21 || 4 |
| 25 || 5 |
----------------
是否可以通过一个查询来完成?
我试过的:
var dates = (from u in db.Users
group u by u.date_of_birth into g
select new { age = calculateAge(g.Key) }).ToList();
var dates1 = from d in dates
group d by d.age into g
select new { age = g.Key, count = g.Count() };
GridView1.DataSource = dates1;
GridView1.DataBind();
这行得通,但我认为有一种方法可以让它更简单。或不?
PS calculateAge 具有以下签名
private int calculateAge(DateTime date_of_birth)