C# 带有驱动程序C的GroupBy Mongodb#
我正在制作一个Statistics模块,我需要做一个GroupBy操作,计算每个国家的总访客数。我正在使用MongoRepository() 我用C#编写的代码运行良好,但我不喜欢这种解决方案:C# 带有驱动程序C的GroupBy Mongodb#,c#,linq,mongodb,C#,Linq,Mongodb,我正在制作一个Statistics模块,我需要做一个GroupBy操作,计算每个国家的总访客数。我正在使用MongoRepository() 我用C#编写的代码运行良好,但我不喜欢这种解决方案: var repositoryIpFrom = new MongoRepository<IpFrom>(); var countries = repositoryIpFrom.Where(s => s.id == id).Select(s => s.Country).Distinc
var repositoryIpFrom = new MongoRepository<IpFrom>();
var countries = repositoryIpFrom.Where(s => s.id == id).Select(s => s.Country).Distinct();
foreach (var country in countries)
{
statisticsCountryDto.Add(new StatisticsCountryDto()
{
Country = country,
Count = repositoryIpFrom.Count(s => s.id == id && s.Country == country)
});
}
return statisticsCountryDto;
我是否可以选择创建GroupBy而不进行大量查询
谢谢 由于您没有对查询进行筛选,因此可以通过插入
AsEnumerable
操作来解析查询,然后在不再涉及MongoDb驱动程序后对本地数据执行分组操作
var tst = repositoryIpFrom
.Where(s=>s.id == id)
.AsEnumerable()
.GroupBy(s => s.Country)
.Select(n => new
{
Country = n.Key,
Count = n.Count()
});
我宁愿使用
AsEnumerable()
。实现列表只是浪费内存。谢谢。用来研究你的建议。
var tst = repositoryIpFrom
.Where(s=>s.id == id)
.AsEnumerable()
.GroupBy(s => s.Country)
.Select(n => new
{
Country = n.Key,
Count = n.Count()
});