草庐IT

ienumerable

全部标签

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

c# - 是否有将 IEnumerator 转换为 IEnumerable 的内置方法

有没有内置的方法来转换IEnumerator至IEnumerable? 最佳答案 我能想到的最简单的转换方式是通过yield语句publicstaticIEnumerableToIEnumerable(thisIEnumeratorenumerator){while(enumerator.MoveNext()){yieldreturnenumerator.Current;}}与列表版本相比,这具有在返回IEnumerable之前不枚举整个列表的优点。使用yield语句你只会迭代你需要的项目,而使用列表版本,你会首先迭代列表中的所有项

c# - 将 IEnumerable<T> 拆分为固定大小的 block (返回 IEnumerable<IEnumerable<T>> ,其中内部序列具有固定长度)

这个问题在这里已经有了答案:CreatebatchesinLINQ(22个答案)SplitListintoSublistswithLINQ(34个答案)关闭5年前。我想参加IEnumerable并将其分成固定大小的block。我有这个,但由于所有的列表创建/复制,它看起来不够优雅:privatestaticIEnumerable>Partition(thisIEnumerableitems,intpartitionSize){Listpartition=newList(partitionSize);foreach(Titeminitems){partition.Add(item);if

c# - 如何检查变量是否是某种 IEnumerable

基本上我正在构建一个非常通用的T4模板,我需要它做的一件事是打印variable.ToString()。但是,我希望它通过它们评估列表和foreach,而不是打印ListItem.ToString()我的T4模板不知道什么类型variable会提前,那这就是为什么它如此通用。但我当前生成的代码如下所示:if(variable!=null)if(variableisIEnumerable)//errorhereforeach(variteminvariable)Write(item.ToString());我在“使用通用类型System.Generic.Collections.IEnum

c# - EF ICollection 与 List 与 IEnumerable 与 IQueryable

因此,我的EF模型具有关系,根据我在示例中看到的内容,这些关系应该使用ICollection的虚拟属性来完成。例子:publicclassTask{publicintId{get;set;}publicstringDescription{get;set;}publicvirtualICollection{get;set;}}我在某处读到我应该使用IEnumerable来防止延迟执行,对吗?这意味着如果我的DAL方法返回IEnumerable,仍然是IQueryable,SQL将在那个时刻执行,而不是在我在网页中调用.TOList的那一刻。那么,最佳做法是什么?我应该返回什么?IEnum