如何访问List <keyvaluepair <string,int>&gt;来自MongoDB

时间:2016-12-05 10:23:02

标签: c# mongodb mongodb-.net-driver

我使用以下定义存储在MongoDB记录中:

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

和以下示例如下:

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)
    }
}

请告诉我如何编写查询以检索Sum xx Data列表中包含KeyValuePair个密钥的所有记录的var TotalXX = collection .Where(r => r.GrId == grId) .GroupBy(t => t.GrId) .Select(v => v.Select(c => c.Data));

我已经从以下声明开始,但不知道下一步应该是什么:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new MainFragment();
ft.replace(R.id.fragment_container, frag);
ft.commit();

1 个答案:

答案 0 :(得分:3)

我认为您不需要GroupBy

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