C# C语言中按int范围过滤字典#

C# C语言中按int范围过滤字典#,c#,dictionary,C#,Dictionary,我有一本字典 Dictionary<int,Observation> 字典 例如,我想创建一个键范围为1到10000的新字典 我得到的是 Dictionary<int,Observation> source; Dictionary<int,Observation> newDict = new Dictionary<int,Observation>(); for (int i = 0; i < 10000; i++) { newDi

我有一本字典

Dictionary<int,Observation>
字典
例如,我想创建一个键范围为1到10000的新字典

我得到的是

Dictionary<int,Observation> source;
Dictionary<int,Observation> newDict = new Dictionary<int,Observation>();
for (int i = 0; i < 10000; i++)
{
    newDict[i] = new Observation(source[i]);         
}
字典源;
字典newDict=新字典();
对于(int i=0;i<10000;i++)
{
newDict[i]=新观察(来源[i]);
}
有没有有效的方法来创建一个只包含适合给定范围(当然是整数)的键的新字典

我是LINQ的新手,但我想有办法做到这一点。

您可以使用以下方法:

var newDict = source.OrderBy(d => d.Key)
                    .Take(10000)
                    .ToDictionary(d=>d.Key,d=>d.Value);
您也可以这样使用:

var newDict = source.OrderBy(d => d.Key)
                    .Skip(1000).Take(10000)
                    .ToDictionary(d=>d.Key,d=>d.Value);
这将
跳过前1000个元素,然后获取10000条记录

或者,您可以使用Where来获取特定范围:

var newDict = source.Where(d => d.Key >= start && d.Key <= end)
                    .ToDictionary(d => d.Key,d => d.Value);
var newDict=source.Where(d=>d.Key>=start&d.Key d.Key,d=>d.Value);
您可以使用:

var newDict = source.OrderBy(d => d.Key)
                    .Take(10000)
                    .ToDictionary(d=>d.Key,d=>d.Value);
您也可以这样使用:

var newDict = source.OrderBy(d => d.Key)
                    .Skip(1000).Take(10000)
                    .ToDictionary(d=>d.Key,d=>d.Value);
这将
跳过前1000个元素,然后获取10000条记录

或者,您可以使用Where来获取特定范围:

var newDict = source.Where(d => d.Key >= start && d.Key <= end)
                    .ToDictionary(d => d.Key,d => d.Value);
var newDict=source.Where(d=>d.Key>=start&d.Key d.Key,d=>d.Value);
试试:

var newDict=source.Keys.Where(c=>c>=rangeMinimum&&cc,c=>source[c]);
其中RangeMimum和rangeMaximum是范围的起点和终点。在您的示例中,0和9999。

请尝试:

var newDict=source.Keys.Where(c=>c>=rangeMinimum&&cc,c=>source[c]);
其中RangeMimum和rangeMaximum是范围的起点和终点。在您的示例中,0和9999。

使用
Where
(具有您想要的任何条件)和
ToDictionary

newDict = source.Where(x => x.Key < 10000 && x.Key > 10)
                                      .ToDictionary(x=>x.Key,x=>x.Value);
newDict=source.Where(x=>x.Key<10000&&x.Key>10)
.ToDictionary(x=>x.Key,x=>x.Value);
使用
Where
(带任何您想要的条件)和
ToDictionary

newDict = source.Where(x => x.Key < 10000 && x.Key > 10)
                                      .ToDictionary(x=>x.Key,x=>x.Value);
newDict=source.Where(x=>x.Key<10000&&x.Key>10)
.ToDictionary(x=>x.Key,x=>x.Value);

是否有任何有效的方法
,这是否意味着您拥有的代码没有效率?不,代码是有效的,但我想在节省其效率的同时使其更美观。
是否有任何有效的方法
,这是否意味着您拥有的代码没有效率?不,代码是有效的,但我想让它更优雅,同时节省它的效率。谢谢。但是有没有办法在这个查询中使用2个范围整数(iStart,iEnd)呢?我不确定,但请检查我的答案,跳过可能有帮助you@Selman22Skip and Take将返回
IEnumerable
not dictionary:)请注意,“1到10.000范围内的键”和“前10.000”键是两个不同的查询。谢谢。但是有没有办法在这个查询中使用2个范围整数(iStart,iEnd)呢?我不确定,但请检查我的答案,跳过可能有帮助you@Selman22Skip and Take将返回
IEnumerable
not dictionary:)请注意,“1到10.000范围内的键”和“前10.000”键是两个不同的查询。谢谢,它正在工作:dObservations=newdictionary(exIn.dObservations.Where(x=>(x.Key=iStart)).ToDictionary(x=>x.Key,x=>x.Value));谢谢,它正在工作:dObservations=newdictionary(exIn.dObservations.Where(x=>(x.Key=iStart)).ToDictionary(x=>x.Key,x=>x.Value));