我有一个包含以下内容的文档:
1> Id: "10204"; FromCity:"Vadodara"; ToCity:"Surat"; OfficeId:{"office1","Office2","Office3"} // row-1
2> Id: "10205"; FromCity:"Ahmedabad"; ToCity:"Surat"; OfficeId:{"office2","Office3","Office4"} // row-2
那么我的输出应该是:
office1 - 1;
Office2 - 2;
Office3 - 2;
Office4 - 1;
请使用 LINQ 帮助我解决这个问题。我已经拆分了 Array 及其单个文档的单个计数。
var OfficeID = results2
.GroupBy(frm => new { frm.ItemNumber },
(key, group) => new
{
Key1 = key.ItemNumber,
Count = group.Sum(e => e.Count)
});
在此先感谢我这样做了:
var OfficeId= results2.SelectMany(@class => @class.ItemNumber)
.GroupBy(s => s).Select(a => new { officeId = a, count = a.Count() });
foreach (var i in OfficeId)
{
Console.WriteLine(i.officeId+"\t"+i.count);
}
但我没有在循环中显示 OfficeId 名称