大约有 50 万个客户的 RavenDB 文档。其中一个属性是“城市”..我如何编写 LINQ 查询来获取每个城市的所有出现及其计数的列表。例如,如果一千个客户文档将“NY”作为城市值,那么我需要一个计数为 NY 1000 的城市列表; LA 200、OR 1300、BO 5000 等.
这是我最初写的..
Dictionary<string,int> cityStats = session.Query<Customer>()
.ToList()
.GroupBy(x => x.City)
.OrderBy(x => x.Count())
.ToDictionary(x => x.Key, x => x.Count());
但这看起来并没有给我准确的结果..所以我更改了允许的最大请求属性(我知道不推荐)只是为了看看它是否改变了结果..但是将 maxrequest 值保持为 500000 也给我带来了相同的结果。我确信大约有 50 万份客户文档,因此需要相加才能匹配。
最佳答案
您需要一个映射缩减索引来执行此操作。这是一个简短的控制台程序,演示了:
using System;
using System.Linq;
using Raven.Client.Document;
using Raven.Client.Indexes;
namespace ConsoleApplication1
{
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
public class Customers_ByCity : AbstractIndexCreationTask<Customer, Customers_ByCity.Result>
{
public Customers_ByCity()
{
Map = customers => from customer in customers
select new
{
customer.City,
Count = 1
};
Reduce = results => from result in results
group result by result.City
into g
select new
{
City = g.Key,
Count = g.Sum(x => x.Count)
};
}
public class Result
{
public string City { get; set; }
public int Count { get; set; }
}
}
class Program
{
private static void Main()
{
var documentStore = new DocumentStore { Url = "http://localhost:8080" };
documentStore.Initialize();
IndexCreation.CreateIndexes(typeof(Program).Assembly, documentStore);
using (var session = documentStore.OpenSession())
{
session.Store(new Customer { Name = "John", City = "NY" });
session.Store(new Customer { Name = "Jane", City = "NY" });
session.Store(new Customer { Name = "Jim", City = "NY" });
session.Store(new Customer { Name = "Sally", City = "LA" });
session.Store(new Customer { Name = "Sam", City = "LA" });
session.Store(new Customer { Name = "Suzie", City = "LA" });
session.Store(new Customer { Name = "Sarah", City = "LA" });
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
// In a real app, you probably don't want to wait for nonstale results.
// You will also want to consider what to do if you have more than one page of results (more than 1024 cities)
var counts = session.Query<Customers_ByCity.Result, Customers_ByCity>()
.Customize(x=> x.WaitForNonStaleResults())
.Take(1024);
foreach (var result in counts)
{
Console.WriteLine("{0}: {1}", result.City, result.Count);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
关于LINQ 查询输出键计数值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589914/