我在通过链接到sql分组时遇到问题,
我有此表:
代码:
justiceDataContext dbContext = new justiceDataContext();
GridView1.DataSource = from client in dbContext.Client
join ville in dbContext.Commande
on client.villeId equals ville.Id_ville
group client by ville.nom into g
select new { City = g.Key, NumberOfClients = g.Count() };
GridView1.DataBind();
我的目标是client
按城市(ville
)获取数量
谢谢你
dbContext.Client
.GroupBy(c => c.villeId)
.Select(g => new {
CityName = dbContext.Villes.Where(v => v.Id_ville == g.Key),
NumberOfClient = g.Count()
}).ToList();
另一种方法:
var result = dbContext.Villes
.Join(dbContextClients, v => v.IdVille, c => c.IdVille, (v, c) => new { client = c, ville = v })
.GroupBy(j => j.ville.IdVille)
.Select(g => new {
VilleName = g.First().ville.Name,
NumberOfClients = g.Count()
}).ToList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句