C# 我可以依赖Linq中SortedDictionary的顺序吗?它能有效地完成这项工作吗?
当在Linq中使用SortedDictionary并对它提供的KeyValuePair进行迭代时,我能保证复杂的Linq查询将以升序执行它吗?下面是一个简短但有点混乱的示例:C# 我可以依赖Linq中SortedDictionary的顺序吗?它能有效地完成这项工作吗?,c#,.net,linq,linq-to-objects,C#,.net,Linq,Linq To Objects,当在Linq中使用SortedDictionary并对它提供的KeyValuePair进行迭代时,我能保证复杂的Linq查询将以升序执行它吗?下面是一个简短但有点混乱的示例: Random r = new Random(); //build 100 dictionaries and put them into a sorted dictionary //with "priority" as the key and it is a number 0-99. SortedDictionary<
Random r = new Random();
//build 100 dictionaries and put them into a sorted dictionary
//with "priority" as the key and it is a number 0-99.
SortedDictionary<int, Dictionary<int, double>> sortedDict =
new SortedDictionary<int, Dictionary<int, double>>();
for (int i = 0; i < 100; i++)
{
Dictionary<int, double> dict = new Dictionary<int, double>();
//create the dictionary and a random 10 k/v pairs
for (int j = 0; j < 10; j++)
{
dict[r.Next(0, 100)] = r.NextDouble() * i * 10;
}
sortedDict[i] = dict;
}
IEnumerable<int> keys = Enumerable.Range(0, 100);
//the goal is to find the FIRST existence of the "key" inside one
//of the inner dictionaries going through the SortedDictionary IN ORDER
//this appears to work:
var qry = from key in keys
from priority in sortedDict
where priority.Value.ContainsKey(key)
let value = priority.Value[key]
group value by key into keyGroup
let firstValue = keyGroup.First()
select new { Key = keyGroup.Key, Value = firstValue };
// the result is as expected, a list of the numbers at most 0-99 and their
// value found in the dictionary with the lowest "priority"
Random r=new Random();
//建立100个字典,并将它们放入一个已排序的字典中
//以“优先级”为键,它是一个数字0-99。
SortedDictionary sortedDict=
新的SortedDictionary();
对于(int i=0;i<100;i++)
{
Dictionary dict=新字典();
//创建字典和随机的10个k/v对
对于(int j=0;j<10;j++)
{
dict[r.Next(0100)]=r.NextDouble()*i*10;
}
sortedDict[i]=dict;
}
IEnumerable键=可枚举的范围(0,100);
//我们的目标是找到其中第一个“钥匙”的存在
//按顺序通过分类词典的内部词典
//这似乎有效:
var qry=从键输入键
从sortedDict中的优先级
其中priority.Value.ContainsKey(键)
让值=优先级。值[键]
按键将值分组为键组
让firstValue=keyGroup.First()
选择新{Key=keyGroup.Key,Value=firstValue};
//结果和预期的一样,一个最多0-99个数字的列表以及它们的
//在字典中找到具有最低“优先级”的值
问题:
var qry = from key in keys
from priority in sortedDict
where priority.Value.ContainsKey(key)
let value = priority.Value[key]
group value by key into keyGroup
select new { Key = keyGroup.Key, Value = keyGroup.First() };
然而,你仍然在用这个浏览字典。你基本上不想要一个从键到包含该键的优先级的反向映射吗?这可以更有效地构建。正如你所说,这个例子相当混乱。如果您能告诉我们您在实际代码中想要实现什么,我们可能会想出更好的方法。(如果确实是这种情况,我当然可以解决它——我宁愿不这样做,然后发现现实是非常不同的!)var qry = from key in keys
from priority in sortedDict
where priority.Value.ContainsKey(key)
let value = priority.Value[key]
group value by key into keyGroup
select new { Key = keyGroup.Key, Value = keyGroup.First() };
然而,你仍然在用这个浏览字典。你基本上不想要一个从键到包含该键的优先级的反向映射吗?这可以更有效地构建。正如你所说,这个例子相当混乱。如果您能告诉我们您在实际代码中想要实现什么,我们可能会想出更好的方法。(如果确实是这种情况,我当然可以解决它——我宁愿不这样做,然后发现现实是非常不同的!) keys.SelectMany(...)
.Where(...)
.GroupBy(...)
.Select(g => g.First())
.Select(...);
所有这些都将以某种方式保持顺序。下面是一个关于linq方法保持顺序的示例
查看查询时,您似乎有:
keys.SelectMany(...)
.Where(...)
.GroupBy(...)
.Select(g => g.First())
.Select(...);
所有这些都将以某种方式保持顺序。实际代码一如既往地更加复杂。我可能会提出另一个问题,因为它不是直接相关的,真正的代码,一如既往,更复杂。我可能会提出另一个问题,因为这不是直接相关的