[英]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
,例如,在KeyValuePair
的Data
列表中具有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.