我使用以下定义存储在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();
答案 0 :(得分:3)
我认为您不需要GroupBy
:
var TotalXX = collection
.Where(storage => storage.GrId == grId)
.Sum(storage => storage.Data.Count(kv => kv.Key == "xx"));