在C#中计算字典中的Distinct值实例的数量

时间:2017-05-15 16:25:59

标签: c# dictionary

我有一个字典,每个键都有多个值。现在,我需要获取该密钥的每个不同值实例的计数。

让我们说

dict(key, list{value}) = ({1,{1,1,2,3,3}},{2,{1,1,1,2,3,3}})

我需要

count of 1 for key 1 : 2
count of 2 for key 1 : 1
count of 3 for key 1 : 2

我需要为字典中的每个键执行此操作。任何帮助是极大的赞赏。感谢。

3 个答案:

答案 0 :(得分:0)

查询将是这样的。假设你有Dictionary<int, List<int>>

var query = dic.SelectMany(kvp => 
                 kvp.Value.GroupBy(v => v)
                          .Select(v => new{ Value = v.Key, Count = v.Count, Key = kvp.Key));

var result = query.ToList();

答案 1 :(得分:0)

这就是我做到的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericValueCounter
{
    public static class GenericHelper<T, U>
    {
        public static Dictionary<U, int> CountValues(Dictionary<T, IEnumerable<U>> dictionary)
        {
            var returnDict = new Dictionary<U, int>();

            foreach (var entry in dictionary)
            {
                foreach (U value in entry.Value)
                {
                    if (!returnDict.ContainsKey(value))
                    {
                        returnDict.Add(value, 1);
                        continue;
                    }
                    else
                    {
                        returnDict[value] = returnDict[value] + 1;
                    }
                }
            }

            return returnDict;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var myDictionary = new Dictionary<int, IEnumerable<int>>();
            myDictionary.Add(1, new[] { 1, 2, 3, 4 });
            myDictionary.Add(2, new[] { 1, 2, 4 });
            myDictionary.Add(5, new[] { 1, 24 });
            myDictionary.Add(7, new[] { 1, 2, 3, 4 });
            myDictionary.Add(8, new[] { 1, 2, 3, 4 });
            myDictionary.Add(9, new[] { 1, 2, 3, 4 });

            var result = GenericHelper<int,int>.CountValues(myDictionary);

            foreach (var item in result)
            {
                Console.WriteLine("Value:{0}, Count:{1}", item.Key, item.Value);
            }
            Console.ReadKey();
        }


    }
}

答案 2 :(得分:0)

也是这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication55
{
    class Program
    {

        static void Main(string[] args)
        {

           Dictionary<int, List<int>>  dict = new Dictionary<int,List<int>>() {
                                   {1,new List<int>(){1,1,2,3,3}},
                                   {2,new List<int> {1,1,1,2,3,3}}};

           foreach (int key in dict.Keys)
           {
               List<int> numbers = dict[key];
               List<int> uniqueNumbers = numbers.Distinct().ToList();
               foreach(int uniqueNumber in uniqueNumbers)
               {
                  Console.WriteLine("count of {0} for key {1} : {2}", key.ToString(), uniqueNumber, numbers.Where(x => x == uniqueNumber).Count()); 
               }

           }

        }


    }

}