2017-05-15 1 views
-3

各キーに複数の値を持つDictionaryを持っています。さて、私はそのキーのそれぞれの個別の値のインスタンスの数を取得する必要があります。C#のディクショナリ内のDistinct valueインスタンスのカウントを取得

は私が私の辞書にすべてのキーのためにこれを行う必要があり

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

が必要

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

言うことができます。どんな助けでも大歓迎です。ありがとう。

+2

あなたは何を試してみましたか?これは[LINQ](https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/concepts/linq/introduction-to-linq-queries)で最も簡単に解決されます。 – Laoujin

+3

構造体の擬似コードは、あなたが試したことを含む[mcve]を提供してください。 (ヒント:LINQは 'ToDictionary'、' Distinct'と 'Count'の組み合わせで、ここでは*ロット*を助けます。) –

+0

私はこの質問に思っています:http://stackoverflow.com/questions/8459928/how-あなたが探しているものを見つけるでしょう辞書 –

答えて

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(); 
+0

ありがとう@kazemakhgary –

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(); 
     } 


    } 
} 
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()); 
       } 

      } 

     } 


    } 

} 
関連する問題