总所周知 在sql中 group by 只能查出 一个字段和数量 例如:products 表的结构为:productid,productname,userid,username,createdate;sql为:select userid,COUNT(userid) as usercount from Products group by useridlinq 为:
var q =
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count()}; 我还想得到 username呢 难道 只能获得 userid 和userid的数量吗我怎么获得 更多的数据呢
var q =
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count()}; 我还想得到 username呢 难道 只能获得 userid 和userid的数量吗我怎么获得 更多的数据呢
解决方案 »
- 是不是现在oracle用.net里的membership不用自己写Provider 了?
- 页面刷新,如何保持服务器控件的状态(在线等~)
- 用js验证控件输入问题。。
- 到www.asp.net里下载的那些Starter Kit怎么是vsi格式的
- Session.Abandon()函数问题
- 向高手请教存储过程中的判断问题:if(exists(select title form table where id=20))出错
- 关于电子地图的问题
- 数据库连接失败么??!!
- 请教一个updatepanel的问题
- sql server连接问题~!
- AJAX购物车的实现请大家帮忙谢谢了。
- AspNetPage控件问题!!!标题要长
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ]
Into aggregateList
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count(), items = g};
//每个items装入了分组中所有的的数据。
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count(),username=g.First().username};
foreach (var g in q)
foreach (var item in q.items)
{
//访问到分组中的元素
}
即要说清楚规则,或直接帖出正确的SQL语句
200000 33333 10109 [email protected]
200001 DT-5001 10106 [email protected]
200002 USTAR USB风扇UF-238 59 [email protected]
200003 USTAR USB风扇UF-215-02 59 [email protected]
200004 USB迷你冰箱UC-688 59 [email protected]
200005 ThinkPad T789i 10161 [email protected]
200006 USTAR USB风扇UF-239 59 [email protected]
200007 USB Cooling 10161 [email protected]
200008 laptop cooling 10161 [email protected]
200000 33333 10109 [email protected]
200001 DT-5001 10106 [email protected]
200002 USTAR USB风扇UF-238 59 [email protected]
200003 USTAR USB风扇UF-215-02 59 [email protected]
200004 USB迷你冰箱UC-688 59 [email protected]
200005 ThinkPad T789i 10161 [email protected]
200006 USTAR USB风扇UF-239 59 [email protected]
200007 USB Cooling 10161 [email protected]
200008 laptop cooling 10161 [email protected]
from p in Products
group p by new{p.userid,p.username} into g
select new {userid=g.Key.userid,usercount=g.Count(),username=g.Key.username};
from p in Products
group p by new{p.userid,p.username} into g
select new {userid=g.Key.userid,usercount=g.Count(),username=g.Key.username};
如果要得到2列,那么可以 group by 这2列,但只能找到两个字段值都一样的数量,得不到每个字段对应的数量,如果这2列是一一对应 那么完全没有问题;sql如下:
select userid,username,COUNT(userid) as usercount from products group by userid,usernamelinq 如下:var q =
from p in Products
group p by new {p1=p.userid,p2=p.username} into g
select new {userid=g.Key.p1,useranem=g.Key.p2,usercount=g.Count()};
我看了他的这个文章 :http://blog.csdn.net/q107770540/article/details/6086577
哪里已经 有答案了。
var q =
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count(),username=g.First().username};
这个方法 更简洁 如果 username和userid 是一一对应 结果完全相同;var q =
from p in Products
group p by p.userid into g
select new {userid=g.Key,usercount=g.Count(), items = g};这个好像也可以 谢谢各位了 结贴了