十年河东,十年河西,莫欺少年穷。
本节探讨的内容很简单,就是如果使用GroupBy计数
提供两种方法:第一:把查询的数据,转化为泛型,然后泛型分组计数。
第二:Linq语句直接分组计数
有如下范例:
SQL如下:
create table S_cate ( cateId int identity(1,1) primary key, cateName varchar(20), ) create table S_info ( Sid int identity(1,1) primary key, cateId int FOREIGN KEY REFERENCES S_cate(cateId), content varchar(100) ) insert into S_cate values('苹果') insert into S_cate values('香蕉') insert into S_cate values('橘子') insert into S_cate values('桃子') insert into S_info values(1,'引用苹果') insert into S_info values(1,'引用苹果') insert into S_info values(1,'引用苹果') insert into S_info values(1,'引用苹果') insert into S_info values(2,'引用香蕉') insert into S_info values(2,'引用香蕉') insert into S_info values(2,'引用香蕉') insert into S_info values(3,'引用橘子') insert into S_info values(3,'引用橘子')
想要的结果为:
橘子:2 苹果:4 桃子:0 香蕉:3
那么用LINQ该如何实现呢?
首先新建返回的数据类型:
public class MSTS { public int cateId { get; set; } public string cateName { get; set; } public int count { get; set; } }
我们采用cateId 和 cateName 联合分组:
LINQ如下:
#region 分组测试 /// <summary> ///LINQ分组示例 /// </summary> /// <returns></returns> public List<MSTS> GetCates() { using (AnbSosCustomerEntities context = new AnbSosCustomerEntities()) { List<MSTS> Mlist = new List<MSTS>(); var S_cate = context.S_cate; var S_info = context.S_info; // var Query = from Cate in S_cate join Info in S_info on Cate.cateId equals Info.cateId into temp from tt in temp.DefaultIfEmpty() select new { cateId=Cate.cateId, cateName = Cate.cateName, content=tt.content }; var data = Query.GroupBy(a => new { a.cateId, a.cateName }).Select(a => new MSTS { cateName = a.Key.cateName, cateId = a.Key.cateId, count = a.Count(C => C.content != null) }); Mlist = data.ToList(); return Mlist; } } #endregion
其实上述的LINQ相信大家都能很快写出来,但是我要强调的是Count('里面的参数')
也就是这一句:
加上这句筛选,就是为了防止将桃子统计为 1
这样调试的结果为:
以上便是第一种方法!
那么第二种方法也很简单,思路是:左连接查询数据,然后把查询的结果转化为泛型,最后利用泛型分组:
在此直接上代码了:
MSTS类变更如下:
public class MSTS { public int cateId { get; set; } public string cateName { get; set; } public string content { get; set; } }
LINQ查询变更如下:
#region 分组测试 /// <summary> ///LINQ分组示例 /// </summary> /// <returns></returns> public List<MSTS> GetCates() { using (AnbSosCustomerEntities context = new AnbSosCustomerEntities()) { List<MSTS> Mlist = new List<MSTS>(); var S_cate = context.S_cate; var S_info = context.S_info; // var Query = from Cate in S_cate join Info in S_info on Cate.cateId equals Info.cateId into temp from tt in temp.DefaultIfEmpty() select new MSTS { cateId = Cate.cateId, cateName = Cate.cateName, content = tt.content }; Mlist = Query.ToList(); return Mlist; } } #endregion
调试实时信息如下:
从调试信息可以看出:因为桃子没被引用过,所以桃子对应的content为null
那么泛型的分组也和linq一样,都是要筛选这个字段的值
泛型分组如下:
IBase Implement = new BaseImplement(); protected void Page_Load(object sender, EventArgs e) { List<MSTS> ls = new List<MSTS>(); ls = Implement.GetCates(); var Gls = ls.GroupBy(a => new { a.cateId,a.cateName}).Select(g => (new { cateName = g.Key.cateName,cateId=g.Key.cateId, count = g.Count(A=>A.content!=null) })); foreach (var item in Gls) { Response.Write(item.cateName + "的数量为:" + item.count + "!"); } }
@陈卧龙的博客