我的表是这样的。
SELECT TOP 1000 [VODID]
,[VODName]
,[VODLocation]
,[VODSets]
,[VODDesc]
,[ProgramFlag]
,[TypeID]
,[GroupID]
,[GroupName]
,[TypeName]
,[Threshold]
FROM [WASUP1V1].[dbo].[V_VODInfo]
这是个视图。
想按VODID来分组,结果失败。
代码是这样的:
int alldatacount = entities.V_VODInfo.GroupBy(a => a.VODID).Count();
得到的结果总是和没有分组前的总是一样的,郁闷,请高手讲解。
最后我写成这样,数据映射成内存对象才能分组,但这样数据量大时会浪费内存。是这样理解吗?
int alldatacount = entities.V_VODInfo.AsQueryable().ToList().GroupBy(a => a.VODID).Count();
List<FWorkBills> fw = new List<FWorkBills>();
fw.Add(new FWorkBills { WorkState = "完工", date = new DateTime(2011, 2, 1, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "完工", date = new DateTime(2011, 2, 2, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "评估", date = new DateTime(2011, 3, 1, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "评估", date = new DateTime(2011, 3, 2, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "评估", date = new DateTime(2011, 3, 3, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "未完工", date = new DateTime(2011, 4, 1, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "未完工", date = new DateTime(2011, 4, 2, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "未完工", date = new DateTime(2011, 4, 3, 0, 0, 0) });
fw.Add(new FWorkBills { WorkState = "未完工", date = new DateTime(2011, 4, 4, 0, 0, 0) });
var Query = from p in fw
group p by p.WorkState[0] into g
select new
{
工程状态 = g.Key,
数量 = g.Count(),
日期=string.Format("{0:yyyy年MM月dd日}", g.ElementAt(0).date)
};
this.bindingSource1.DataSource = Query;
this.dataGridView1.DataSource = this.bindingSource1