I had error from line
Dictionary<int, string> viewBusinesAndCountLeft = grptemp.Take(4).ToDictionary(x => x.Count, x => x.BusinessCategoryName);
error: “The element with the same key has already been added.”
How to do this?
var grptemp = (from adsBusines in m_adsRepository.SaleBusinesAds
group adsBusines by adsBusines.BusinessCategory.Name
into grp
select new
{
BusinessCategoryName = grp.Key,
Count = grp.Select(x => x.BusinessCategory.ChildItems.Count()).Distinct().Count()
}).Take(8);
Dictionary<int, string> viewBusinesAndCountLeft = grptemp.Take(4).ToDictionary(x => x.Count, x => x.BusinessCategoryName);
Dictionary<int, string> viewBusinesAndCountRigth = grptemp.Skip(4).Take(4).ToDictionary(x => x.Count, x => x.BusinessCategoryName);
You are using the count as the key for the dictionary which means you will throw that exception whenever you happen to find two categories with the same count.
If I understand what you’re trying to do correctly, you should have the ‘business category’ as the key to the dictionary and the count as the value.
E.g.