c# – 使用子集合在RavenDb中映射2个集合

前端之家收集整理的这篇文章主要介绍了c# – 使用子集合在RavenDb中映射2个集合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两种不同的对象类型存储在RavenDb中,它们是父/子类型关系,在 JSON中是这样的:
  1. Account/1
  2. {
  3. "Name": "Acc1",}
  4.  
  5. Items/1
  6. {
  7. "Account": "Account/1","Value" : "100","Tags": [
  8. "tag1","tag2"]
  9. }
  10.  
  11. Items/2
  12. {
  13. "Account": "Account/1","Value" : "50","Tags": [
  14. "tag2"]
  15. }

请注意,我不想将它们存储在同一文档中,因为一个帐户可能有数千个项目.

我正在尝试编写一个map / reduce索引,它会返回类似于:

  1. {
  2. "Account": "Acc1","TagInfo": [
  3. { "TagName" : "tag1","Count" : "1",//Count of all the "tag1" occurrences for acc1
  4. "Value" : "100" //Sum of all the Values for acc1 which are tagged 'tag1'
  5. },{ "TagName" : "tag2","Count" : "2",//Two items are tagged "tag2"
  6. "Value" : "150"
  7. }]
  8. }

即所有不同标签名称的列表,以及每个标签名称数量及其值.

我想我需要使用多地图将Account和Items集合映射到一起,但我无法找出reduce部分来创建结果的“TagInfo”部分.

这是可能的,还是我在Raven模拟这个错误

编辑:

我想从这个查询中检索的类看起来像这样:

  1. public class QueryResult
  2. {
  3. public string AccountId {get;set;}
  4. public TagInfo Tags {get;set;}
  5. }
  6.  
  7. public class TagInfo
  8. {
  9. public string TagName {get;set;}
  10. public int Count {get;set;}
  11. public int TotalSum {get;set;}
  12. }

解决方法

您不能使用Multi Map / Reduce索引,因为您需要在标签上放置一个地图而在帐户上放置另一个地图.他们没有共同的属性,所以你不能在这里有多个地图/减少.

但是,您可以使用TransformResult.这是怎么做的:

  1. public class Account
  2. {
  3. public string Id { get; set; }
  4. public string Name { get; set; }
  5. }
  6.  
  7. public class Item
  8. {
  9. public string Id { get; set; }
  10. public string AccountId { get; set; }
  11. public int Value { get; set; }
  12. public List<string> Tags { get; set; }
  13. }
  14.  
  15. public class TagsWithCountAndValues : AbstractIndexCreationTask<Item,TagsWithCountAndValues.ReduceResult>
  16. {
  17. public class ReduceResult
  18. {
  19. public string AccountId { get; set; }
  20. public string AccountName { get; set; }
  21. public string Tag { get; set; }
  22. public int Count { get; set; }
  23. public int TotalSum { get; set; }
  24. }
  25.  
  26. public TagsWithCountAndValues()
  27. {
  28. Map = items => from item in items
  29. from tag in item.Tags
  30. select new
  31. {
  32. AccountId = item.AccountId,Tag = tag,Count = 1,TotalSum = item.Value
  33. };
  34. Reduce = results => from result in results
  35. group result by result.Tag
  36. into g
  37. select new
  38. {
  39. AccountId = g.Select(x => x.AccountId).FirstOrDefault(),Tag = g.Key,Count = g.Sum(x => x.Count),TotalSum = g.Sum(x => x.TotalSum)
  40. };
  41. TransformResults = (database,results) => from result in results
  42. let account = database.Load<Account>(result.AccountId)
  43. select new
  44. {
  45. AccountId = result.AccountId,AccountName = account.Name,Tag = result.Tag,Count = result.Count,TotalSum = result.TotalSum
  46. };
  47. }
  48. }

之后,您可以像这样查询

  1. var results = session.Query<TagsWithCountAndValues.ReduceResult,TagsWithCountAndValues>()
  2. .Where(x => x.AccountId == "accounts/1")
  3. .ToList();

猜你在找的C#相关文章