读书人

linq 中 g.first()怎么跟g.count() 连

发布时间: 2012-08-30 09:55:54 作者: rapoo

linq 中 g.first()如何跟g.count() 连用啊?
数据表products结构如下:

id name userid
001 兔子 23
002 小狗 23
005 驴 21
008 大象 23
009 老虎 24
如图 这样的数据;
我想每个用户 只取第一条数据;但是要要现实 这个用户有几个商品;

想得到这样的结果:
id name userid count
001 兔子 23 3
005 驴 21 1
009 老虎 24 1
有什么办法吗??




[解决办法]
var list = from dr in dt.AsEnumerable()
group dr by dr.Field<string>("userid") into g
select new { userid=g.Key,count=g.Count(),firstItem=g.First()};

[解决办法]
var s = from temp in dc.Class1 group temp by temp.userid into g select new { id = g.First().id, name = g.First().name, userid = g.Key ,count = g.Count() };

[解决办法]
这个是group by 效果的。

上网随便一搜就找到答案。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
Total = g.Count(p => p.UnitPrice)
};
[解决办法]

C# code
var list = from dr in dt.AsEnumerable()                       group dr by dr.Field<string>("userid") into g                       select new { userid=g.Key,count=g.Count(),productlist=g.FirstOrDefault()}; 

读书人网 >asp.net

热点推荐