简体   繁体   English

如何访问列表 <KeyValuePair<string,int> &gt;来自MongoDB

[英]How can I access List<KeyValuePair<string,int>> from MongoDB

I am storing in MongoDB records with the following definition: 我将以下定义存储在MongoDB记录中:

public class StorageRecord
{
    public int GrId { get; set; }
    public List<KeyValuePair<string,int>> Data { get; set; }
}

and sample like this one below: 并在下面示例如下:

var storageRecord = new StorageRecord
{
GrId = 12,
Data = new List<KeyValuePair<string, int>>()
    {
        new KeyValuePair<string, int>("xx", 12),
        new KeyValuePair<string, int>("yy", 13),
        new KeyValuePair<string, int>("zz", 14)
    }
}

Please tell me how should I write the query to retrive the Sum of all of the records which has for example xx key in the Data list of KeyValuePair . 请告诉我如何编写查询以检索所有记录的Sum ,例如,在KeyValuePairData列表中具有xx键。

I have started with the following statement but don't know what should be next: 我从下面的语句开始,但不知道下一步应该是什么:

var TotalXX = collection
            .Where(r => r.GrId == grId)
            .GroupBy(t => t.GrId)
            .Select(v => v.Select(c => c.Data));

I think you don't need the GroupBy : 我认为您不需要GroupBy

var TotalXX = collection
   .Where(storage => storage.GrId == grId)
   .Sum(storage => storage.Data.Count(kv => kv.Key == "xx"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成列表 <KeyValuePair<string, int> &gt;由另一个列表中的分组组成? - How can I generate a List<KeyValuePair<string, int>> consisting of groupings from another list? 如何缩短列表 <List<KeyValuePair<string, string> &gt;&gt;? - How can I shorten List<List<KeyValuePair<string, string>>>? 我如何检查List &lt;键值对中的多个键 <string,string> &gt;? - how i can check multiple keys in List< keyvaluepair <string,string>>? 如何将int数组转换为List <KeyValuePair<int, string> &gt;? - How to convert int array to List<KeyValuePair<int, string>>? 如何正确使用列表上的LINQ查询 <KeyValuePair<string, string> 创建字典 <KeyValuePair<string, string> ,int&gt;? - How to properly use LINQ Query on List<KeyValuePair<string, string> to create Dictionary<KeyValuePair<string, string>, int>? 如何转换清单 <KeyValuePair<int,int> &gt;转换为int [] []? - How to convert List<KeyValuePair<int,int>> to int[][]? 将string []转换为List <KeyValuePair<int, string> &gt; - Turn string[] into List<KeyValuePair<int, string>> 如何获取(字符串,列表)的键值对列表的内部列表值? - How can I get the inner list value of list of keyvaluepair of (string, list)? 如何将值设置为:KeyValuePair <KeyValuePair <String,String>,int> - How to set the values to: KeyValuePair<KeyValuePair<String,String>,int> 定义一个KeyValuePair结构,例如 <int,List<string> &gt; - Define a KeyValuePair struct like <int,List<string>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM