C# 如何在不使用LINQ的情况下获得字典中具有相同值的所有键
我有这个:C# 如何在不使用LINQ的情况下获得字典中具有相同值的所有键,c#,dictionary,C#,Dictionary,我有这个: Dictionary<integer, string> dictTempKeys = new Dictionary<integer, string>(); 已经给出了使用LINQ的答案,但我不想在代码中使用LINQ,因为我将在C#CLR存储过程中使用此代码,而这不支持LINQ。那么,我还有其他方法可以做到这一点吗?您可以使用一个简单的foreach循环并反转您的字典: Dictionary<string, List<int>> key
Dictionary<integer, string> dictTempKeys = new Dictionary<integer, string>();
已经给出了使用LINQ的答案,但我不想在代码中使用LINQ,因为我将在C#CLR存储过程中使用此代码,而这不支持LINQ。那么,我还有其他方法可以做到这一点吗?您可以使用一个简单的
foreach
循环并反转您的字典:
Dictionary<string, List<int>> keysByValue = new Dictionary<string, List<int>>();
foreach (KeyValuePair<int, string> entry in dictTempKeys {
List<int> keys;
bool hasKeys = keysByValue.TryGet(entry.Value, out keys);
if (!hasKeys) {
keys = new List<int>();
keysByValue.Add(entry.Value, keys);
}
keys.Add(entry.Key);
}
Dictionary keysByValue=new Dictionary();
foreach(dictTempKeys中的KeyValuePair条目{
列出键;
bool hasKeys=keysByValue.TryGet(entry.Value,out键);
如果(!haskey){
keys=新列表();
添加(entry.Value,keys);
}
Key.Add(entry.Key);
}
您可以使用一个简单的foreach
循环并反转字典:
Dictionary<string, List<int>> keysByValue = new Dictionary<string, List<int>>();
foreach (KeyValuePair<int, string> entry in dictTempKeys {
List<int> keys;
bool hasKeys = keysByValue.TryGet(entry.Value, out keys);
if (!hasKeys) {
keys = new List<int>();
keysByValue.Add(entry.Value, keys);
}
keys.Add(entry.Key);
}
Dictionary keysByValue=new Dictionary();
foreach(dictTempKeys中的KeyValuePair条目{
列出键;
bool hasKeys=keysByValue.TryGet(entry.Value,out键);
如果(!haskey){
keys=新列表();
添加(entry.Value,keys);
}
Key.Add(entry.Key);
}
以下是我的尝试-一种通用扩展方法。
第一个foreach
循环遍历源字典并将其转换为查找,其中键是来自源的值,值是指示源字典中源值的出现计数的整数
第二个foreach
循环将使用yield-return
语句为您提供一个IEnumerable
public static IEnumerable<TValue> GetNonUniqueValues<TKey, TValue>(this IDictionary<TKey, TValue> source)
{
Dictionary<TValue, int> results = new Dictionary<TValue, int>();
foreach (var kvp in source)
{
int count;
if (results.TryGetValue(kvp.Value, out count))
{
count++;
}
else
{
count = 1;
}
results[kvp.Value] = count;
}
foreach (var kvp in results)
{
if (kvp.Value > 1)
{
yield return kvp.Key;
}
}
}
public static IEnumerable GetUnuniqueValues(此IDictionary源代码)
{
字典结果=新字典();
foreach(源中的var kvp)
{
整数计数;
if(结果TryGetValue(kvp.Value,超出计数))
{
计数++;
}
其他的
{
计数=1;
}
结果[kvp.值]=计数;
}
foreach(结果中的var kvp)
{
如果(kvp.Value>1)
{
产生返回kvp.Key;
}
}
}
以下是我的尝试-一种通用扩展方法。
第一个foreach
循环遍历源字典并将其转换为查找,其中键是来自源的值,值是指示源字典中源值的出现计数的整数
第二个foreach
循环将使用yield-return
语句为您提供一个IEnumerable
public static IEnumerable<TValue> GetNonUniqueValues<TKey, TValue>(this IDictionary<TKey, TValue> source)
{
Dictionary<TValue, int> results = new Dictionary<TValue, int>();
foreach (var kvp in source)
{
int count;
if (results.TryGetValue(kvp.Value, out count))
{
count++;
}
else
{
count = 1;
}
results[kvp.Value] = count;
}
foreach (var kvp in results)
{
if (kvp.Value > 1)
{
yield return kvp.Key;
}
}
}
public static IEnumerable GetUnuniqueValues(此IDictionary源代码)
{
字典结果=新字典();
foreach(源中的var kvp)
{
整数计数;
if(结果TryGetValue(kvp.Value,超出计数))
{
计数++;
}
其他的
{
计数=1;
}
结果[kvp.值]=计数;
}
foreach(结果中的var kvp)
{
如果(kvp.Value>1)
{
产生返回kvp.Key;
}
}
}
您可以使用一个简单的loop@BugFinder但是它会破坏字典的主要用途,破坏它的速度。我可以使用任何扩展方法吗?如果没有完全扫描,这是不可能的。字典
中的值没有索引,只有键。“我只想要那些在键中出现多次的值”-什么原因?闻起来像是X-Y问题。是的,但用它做什么?你可以用一个简单的loop@BugFinder但是它会破坏字典的主要用途,它的速度。有什么扩展方法我可以使用吗?没有完整的扫描是不可能的。字典中的值没有索引,只有键是。“我只想要那些在dictTempKeys中多次出现的值”-出于什么原因?闻起来像是X-Y问题。是的,但用它做什么?