您不能製作List<string, double>
,因爲List<T>
只需要一個類型參數。試試這個:
var results =
(from c in db.Customers
from o in c.Origins
group o by o.DisplayName into g
let total = db.Customers.Count()
select new
{
Origin = g.Key,
Percent = ((double)g.Count()/(double)total) * 100
})
.ToList();
這有(前各產地一次)計算單個客戶多次的可能性,這樣你就不會出來與合計100%的列表。
如果你想在CustomerOrigin
記錄的百分比,試試這個
var results =
(from c in db.Customers
from o in c.Origins
group o by o.DisplayName into g
let total = db.Customers.Sum(x => x.Origins.Count())
select new
{
Origin = g.Key,
Percent = ((double)g.Count()/(double)total) * 100
})
.ToList();
哦,是的,正確的用'名單<字符串,雙>'。在你的查詢中,我得到了這個異常:'base {System.SystemException} = {「調用'GroupBy'方法的鍵選擇器類型在底層存儲提供者中沒有可比性。'}' – SeToY
'c.Origins '會是一個集合(n:m關係),這就是爲什麼在那裏沒有'OriginName'屬性。我嘗試c組由c.CustomerOrigins.Select(co => co.Origin)通過c.CustomerOrigins.Select(co => co.Origin).g選擇(o => o.DisplayName)到g'但無濟於事 – SeToY
@SeToY好吧我覺得我現在明白了更好 - 看我更新的回答 –