【发布时间】:2021-11-16 12:55:43
【问题描述】:
我想显示一个列表,其中包含所有者名称和每个列表的数量。 group.Key 给了我这个人的 id,但我怎样才能把它换成名字?
// Get all lists owned by an organization
var lists = context.SalesToolsLists.Include(x=>x.OwningUserOrganization).Where(x => uoids.Contains(x.OwningUserOrganization.Id));
// Get a dataset where each row contains OwningUserOrganization.FullName, group.Key and group.Count()
res.UOListCounts = new List<Tuple<int, int>>();
foreach (var listgroup in lists?.GroupBy(info => info.OwningUserOrganizationId).Include(x=>x)
.Select(group => new
{
Metric = group.Key,
Count = group.Count(),
}).OrderBy(x => x.Metric))
{
res.UOListCounts.Add(new Tuple<int, int>(listgroup.Metric, listgroup.Count));
}
【问题讨论】:
-
GroupBy(info => new { id = info.OwningUserOrganizationId, name = info.Name})假设名称在 info 变量中 -
附注(这是我的观点),将查询移出 foreach 并移入自己的变量可能会提高代码的可读性。
-
感谢所有帮助! gunr2171,没错!