草庐IT

ienumerator

全部标签

c# - 在返回 IEnumerable<T> 的方法中使用 IDisposable 对象

假设您有一个内部使用IDisposable对象(例如流读取器)的方法,并且yield返回从文件中读取的项目。像这样:publicIEnumerableRead(stringfilename){using(varfilestream=newFileStream(filename,FileMode.Open)){using(varreader=newStreamReader(filestream)){stringline;while((line=reader.ReadLine())!=null){yieldreturnnewYourObject(line);}}}}当我使用不迭代完整集合的

c# - yield break 是否等同于从返回 IEnumerable<T> 的方法返回 Enumerable<T>.Empty

这两种方法对我来说似乎是一样的publicIEnumerableGetNothing(){returnEnumerable.Empty();}publicIEnumerableGetLessThanNothing(){yieldbreak;}我在测试场景中分析了每一个,我没有发现速度上有什么明显的差异,但是yieldbreak版本稍微快一些。是否有理由使用一个而不是另一个?一个比另一个更容易阅读吗?是否存在对调用者重要的行为差异? 最佳答案 如果你打算总是返回一个空的枚举然后使用Enumerable.Empty()语法更具声明性恕我

c# - 无法使用集合初始值设定项初始化类型 '',因为它未实现 'System.Collections.IEnumerable'

我创建了一个类,其中包含三个类作为属性:publicclassFeeds{publicRentalsRentals{get;set;}publicAgentAgents{get;set;}publicNorthwindService.ServiceReference1.FileFile{get;set;}}我是这样使用它的:varquery=fromrinent.Rentalsjoinainent.Agentsonr.ListingAgentIDequalsa.AgentIDselectnewFeeds{a.AgentID,a.Alias,a.Bio,a.Email,a.Fax,r.F

c# - 为什么 IEnumerable<T> 在 C# 4 中是协变的?

在早期版本的C#中IEnumerable定义如下:publicinterfaceIEnumerable:IEnumerable从C#4开始,定义是:publicinterfaceIEnumerable:IEnumerable是否只是为了让LINQ表达式中烦人的转换消失?这不会引入与string[]相同的问题吗?在C#中(损坏的数组方差)?从兼容性的角度来看,如何添加协方差?较早的代码是否仍适用于较新版本的.NET或此处是否需要重新编译?反过来呢?以前使用此接口(interface)的代码是否在所有情况下都严格不变,或者某些用例现在的行为可能会有所不同? 最佳

c# - 无法将类型 'System.Collections.Generic.IEnumerable<AnonymousType#1>' 隐式转换为 'System.Collections.Generic.List<string>

我有以下代码:Listaa=(fromcharcinsourceselectnew{Data=c.ToString()}).ToList();但是呢Listaa=(fromcharc1insourcefromcharc2insourceselectnew{Data=string.Concat(c1,".",c2)).ToList();编译出错Cannotimplicitlyconverttype'System.Collections.Generic.List'to'System.Collections.Generic.List'需要帮助。 最佳答案

c# - 将 DataTable 转换为 IEnumerable<T>

我正在尝试将DataTable转换为IEnumerable。其中T是我创建的自定义类型。我知道我可以通过创建List来做到这一点但我在想是否有一种更巧妙的方法可以使用IEnumerable来做到这一点。这是我现在拥有的:privateIEnumerableConvertToTankReadings(DataTabledataTable){vartankReadings=newList();foreach(DataRowrowindataTable.Rows){vartankReading=newTankReading{TankReadingsID=Convert.ToInt32(row

c# - 从 IEnumerable<T> 集合中移除项目

我有一个填充的IEnumerable收藏。我想从中删除一个项目,我该怎么做?foreach(varuinusers){if(u.userId=1123){//remove!}}我知道你不应该在循环时删除,所以我不介意创建一个新集合或在之后删除它。但我不知道如何删除一个项目,由于某种原因丢失了!或者我也很困惑,我怎样才能创建一个新的集合,比如:IEnumerablemodifiedUsers=newList();foreach(varuinusers){if(u.userId!=1233){modifiedUsers.add??????}}如何添加到收藏中?

c# - 合并两个 IEnumerable<T>

我有两个IEnumerable其中充满了后备元素。这一个将始终包含最多的元素。另一个将根据某些参数进行填充,并且可能包含较少的元素。如果第二个元素不存在,我需要用第一个元素的等效元素填充它。这段代码完成了工作,但我觉得效率低下,需要我将IEnumerables转换为ILists或使用临时列表人实现IEquatableIEnumerablefallBack=Repository.GetPersons();IListtranslated=Repository.GetPersons(language).ToList();foreach(PersonpersoninfallBack){if(!

c# - 我应该使用什么 IEnumerable 或 IList?

这个问题在这里已经有了答案:IListvsIEnumerableforCollectionsonEntities(2个答案)关闭9年前。谁能告诉我什么时候应该使用。例如,我认为当我想访问集合或单个项目的.Count时应该使用IList,对吗?谢谢。

c# - 附加/连接两个 IEnumerable 序列

我有两组数据行。它们都是IEnumerable。我想将这两个列表附加/连接到一个列表中。我确定这是可行的。我不想做一个for循环并注意到两个列表上有一个Union方法和一个Join方法。有什么想法吗? 最佳答案 假设您的对象属于同一类型,您可以使用Union或Concat。请注意,与SQLUNION关键字一样,Union操作将确保消除重复项,而Concat(如UNIONALL)将简单地将第二个列表添加到第一个列表的末尾。IEnumerablefirst=...;IEnumerablesecond=...;IEnumerablecom