我想顯示一個串列,其中包含所有者姓名和每個串列的數量。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));
}
uj5u.com熱心網友回復:
您只需要按名稱對專案進行分組,而不是OwningUserOrganizationId如下所示:
foreach (var listgroup in lists?.GroupBy(info => info.OwningUserOrganization.FullName).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));
}
所以這group.Key給OwningUserOrganization.FullName
正如@gunr2171在評論中指出的那樣,您可以將查詢從 foreach 移到它自己的變數中,以提高代碼的可讀性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360037.html
上一篇:EFCore多對多查詢連接3個表
