我有一个字典,我想写一个方法来检查这个字典中的所有值是否相同.字典类型:
Dictionary<string, List<string>>
Run Code Online (Sandbox Code Playgroud)
在我的案例中,列表{1,2,3}和{2,1,3}是相同的.
我之前已经为简单的数据类型值做了这个,但我找不到新要求的逻辑,请帮助我.对于简单值:MyDict.GroupBy(x => x.Value).Where(x => x.Count()> 1)
我还编写了一个通用方法来以这种方式比较两种数据类型.
// 1
// Require that the counts are equal
if (a.Count != b.Count)
{
return false;
}
// 2
// Initialize new Dictionary of the type
Dictionary<T, int> d = new Dictionary<T, int>();
// 3
// Add each key's frequency from collection A to the Dictionary
foreach (T item in a)
{
int c;
if (d.TryGetValue(item, out c))
{
d[item] = c + 1;
}
else
{
d.Add(item, 1); …Run Code Online (Sandbox Code Playgroud) 我需要找出字典值的联盟.我在下面创建了字典.
Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();
List<string> ls1 = new List<string>();
ls1.Add("1");
ls1.Add("2");
ls1.Add("3");
ls1.Add("4");
List<string> ls2 = new List<string>();
ls2.Add("1");
ls2.Add("5");
dict.Add(1, ls1);
dict.Add(2, ls2);
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下输出将是{"1","2","3","4","5"}