jjzjj

enumerate

全部标签

c# - 为什么 Enumerable.Empty() 返回一个空数组?

我希望Enumerable.Empty()的实现是这样的:publicstaticIEnumerableEmpty(){yieldbreak;}但是实现是这样的:publicstaticIEnumerableEmpty(){returnEmptyEnumerable.Instance;}internalclassEmptyEnumerable{privatestaticvolatileTElement[]instance;publicstaticIEnumerableInstance{get{if(EmptyEnumerable.instance==null)EmptyEnumerab

c# - List<T>.Enumerator 的 Reset 方法的行为

以下两种方法(一种使用IEnumerator,另一种使用List.Enumerator)即使看起来相同会产生不同的结果。staticvoidM1(){varlist=newList(){1,2,3,4};IEnumeratoriterator=list.GetEnumerator();while(iterator.MoveNext()){Console.Write(iterator.Current);}iterator.Reset();while(iterator.MoveNext()){Console.Write(iterator.Current);}}staticvoidM2(){

c# - 如何解决 Enumerable 和 MoreLINQ 之间不明确的 ZIP 调用?

我遇到了扩展方法解析的问题。LINQ和MoreLINQ包含zip方法,它自4.0版本以来就存在于.NET中,并且始终在MoreLINQ中。图书馆。但是您不能使用带有旧式扩展方法语法的实现之一。所以这段代码不会编译usingMoreLinq;usingSystem.Linq;varstudents=new[]{"Mark","Bob","David"};varcolors=new[]{"Pink","Red","Blue"};students.Zip(colors,(s,c)=>s+c);错误:Thecallisambiguousbetweenthefollowingmethodsorp

c# - 为什么 Enumerable.Except 返回不同的项目?

刚刚花了一个多小时调试我们代码中的一个错误,最终证明是关于Enumerable.Except的错误。我们不知道的方法:varilist=new[]{1,1,1,1};varilist2=Enumerable.Empty();ilist.Except(ilist2);//returns{1}asopposedto{1,1,1,1}或更一般地说:varilist3=new[]{1};varilist4=new[]{1,1,2,2,3};ilist4.Except(ilist3);//returns{2,3}asopposedto{2,2,3}查看MSDN页面:Thismethodretur

c# - LINQ to Entities/LINQ to SQL : switching from server (queryable) to client (enumerable) in the middle of a query comprehension?

在许多情况下,我想在服务器端进行一些过滤(有时是投影),然后切换到客户端以执行LINQ提供程序本身不支持的操作。天真的方法(这基本上就是我现在所做的)是将其分解为多个查询,类似于:varfromServer=fromtincontext.Tablewheret.Col1=123wheret.Col2="blah"selectt;varclientSide=fromtinfromServer.AsEnumerable()wheret.Col3.Split('/').Last()=="whatever"selectt.Col4;但是,很多时候,这带来的代码/麻烦多于它的实际值(value)

c# - 帮助理解 Enumerable.Join 方法

昨天我postedthisquestion关于在Join()方法中使用lambda来检查2个实体中是否存在2个条件。我收到了关于这个问题的答案,效果很好。我想在阅读了关于Enumerable.Join()方法的MSDN文章之后,我会确切地理解发生了什么,但我没有。有人可以帮我理解下面代码中发生了什么(特别是Join()方法)吗?提前致谢。if(db.TableA.Where(a=>a.UserID==currentUser).Join(db.TableB.Where(b=>b.MyField==someValue),o=>o.someFieldID,i=>i.someFieldID,(

c# - 为什么 Enumerable.Range 实现 IDisposable?

只是想知道为什么Enumerable.Range工具IDisposable.我明白为什么IEnumerator确实如此,但是IEnumerable不需要它。(我在玩我的.Memoise()实现时发现了这一点,它有类似的语句if(enumerableisIDisposable)((IDisposable)enumerable).Dispose();出于好奇,我在它的“sourcefinished”方法中放置了一个断点,并由测试触发。) 最佳答案 Enumerable.Range在其方法主体中使用yieldreturn。yieldret

c# - Enumerable.Average 和 OverflowException

也许是一个无用的问题:publicstaticdoubleAverage(thisIEnumerablesource,Funcselector)上述方法抛出的异常之一也是OverflowException:序列中元素的总和大于Int64.MaxValue。我假设此异常的原因是平均值的总和是使用long类型的变量S计算的?但是既然返回值是double类型,为什么设计者不选择让S也是double类型呢?谢谢 最佳答案 因为这个特定的重载知道您开始使用int值,所以它知道您没有使用十进制值。将您的每个值转换为double然后将double

c# - C#中 'enumerator'的定义

C#中的枚举数是什么意思? 最佳答案 枚举器可帮助您枚举(迭代)项目集合。您可以通过简单地查看membersoftheIEnumeratorInterface来推断其用途。.更具体地说,枚举器确切地知道您在集合中的位置(当前项)和下一项的位置(MoveNext方法)。查看关于迭代器的维基百科文章:Iterator-Wikipedia 关于c#-C#中'enumerator'的定义,我们在StackOverflow上找到一个类似的问题: https://stac

c# - 为什么 Enumerable.Range 比直接 yield 循环更快?

下面的代码正在检查执行相同解决方案的三种不同方法的性能。publicstaticvoidMain(string[]args){//forloop{Stopwatchsw=Stopwatch.StartNew();intaccumulator=0;for(inti=1;iaccumulator+n);sw.Stop();Console.WriteLine("time={0};result={1}",sw.ElapsedMilliseconds,ret);}//self-madeIEnumerable{Stopwatchsw=Stopwatch.StartNew();varret=GetI