草庐IT

ienumerable

全部标签

c# - 对 IEnumerable 中的先前值求和

我有一个数字序列:varseq=newList{1,3,12,19,33};我想将其转换为一个新序列,其中将数字添加到前面的数字以创建一个新序列:{1,3,12,19,33}-->{1,4,16,35,68}我想到了以下内容,但我不喜欢状态变量“count”。我也不喜欢这样的事实,即我使用Enumerable值而不对其进行操作。intcount=1;varsummed=values.Select(_=>values.Take(count++).Sum());还能怎么办? 最佳答案 这是函数式编程中的常见模式,在F#中称为scan.这

c# - Linq 从两个列表中返回所有元素对?

给定列表l1={1,2}和l2={4,5,6}我想得到一个包含元素的新列表:rez={{1,4},{1,5},{1,6},{2,4},{2,5},{2,6}}建议? 最佳答案 是的,这是可能的。EricLippert就此主题写了一篇非常好的文章:ComputingaCartesianProductwithLINQ如果你只有2个列表,那么你可以像这样直接使用多个from:fromains1frombins2selectnew[]{a,b};甚至:s1.SelectMany(a=>s2.Select(b=>new[]{a,b}));但E

c# - 在 Lambda/LINQ 中组合列表

如果我有IEnumerable>类型的变量是否有LINQ语句或lambda表达式我可以应用于它,它将组合返回IEnumerable的列表? 最佳答案 SelectMany-即IEnumerable>someList=...;IEnumerableall=someList.SelectMany(x=>x);对于someList中的每个项目,这然后使用lambda“x=>x”为内部项目获取IEnumerable。在这种情况下,每个“x”都是一个List,它已经是IEnumerable。然后将这些作为连续block返回。本质上,Selec

C# List<T> 与 IEnumerable<T> 性能问题

你好,假设这两种方法:privateListGetProviderForType(Typetype){ListreturnValue=newList();foreach(KeyValuePairproviderin_objectProviders){if((provider.Key.IsAssignableFrom(type)||type.IsAssignableFrom(provider.Key))&&provider.Value.SupportsType(type)){returnValue.Add(provider.Value);}}returnreturnValue;}priv

c# - LINQ:从字典中获取给定值列表的键,反之亦然

我的代码中有以下结构Dictionarydata;.我对这两种数据类型都运行了一些LINQ查询,并且经常需要在Keys之间切换和Values.获取给定值的键列表的最佳方法是什么,反之亦然?请注意,由于我以前的LINQ查询,我通常有'IEnumerable'和'IEnumerable'并且想要像IEnumerableDictionary.GetAllKeys(IEnumerablevals)这样的东西和IEnumerableDictionary.GetAllValues(IEnumerablekeys).也许我需要其他数据容器来完成这项任务?问候,亚历山大。

c# - IEnumerable 是否需要使用 foreach 循环?

这个问题在这里已经有了答案:DoesClassneedtoimplementIEnumerabletouseForeach(11个答案)关闭9年前。我想知道,我到底什么时候可以使用foreach循环?我必须实现IEnumerable吗?

C# Linq - 无法将 IEnumerable<string> 隐式转换为 List<string>

我有一个这样定义的列表:publicListAttachmentURLS;我正在像这样向列表中添加项目:instruction.AttachmentURLS=curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment=>!String.IsNullOrEmpty(Attachment));但我收到此错误:无法将IEnumerable隐式转换为列表我做错了什么? 最佳答案 Where方法返回IEnumerable.尝试添加.ToLis

C# - IEnumerable 到定界字符串

转换IEnumerable的函数式编程方法是什么?到分隔字符串?我知道我可以使用循环,但我正在尝试围绕函数式编程。这是我的例子:varselectedValues=fromListItemitemincheckboxList.Itemswhereitem.Selectedselectitem.Value;vardelimitedString=??..或者我可以只在第一个var赋值中这样做(将每个结果附加到前一个结果)? 最佳答案 string.Join(",",string[]enumerable)

c# - 无法从用法中推断出开放泛型类型参数

在这个问题中,当提到编译器时,我实际上指的是Roslyn编译器。使用IntelliSense时会出现问题,这被认为是相同的编译器。出于演示目的和完整性,使用了以下类(使用带有C#6.0和.NET4.6.1的VisualStudio2015):publicclassA{publicIEnumerableB{get;set;}}publicclassB{publicIEnumerableC{get;set;}}publicclassC{}publicclassHelper{}看下面的扩展方法:publicstaticvoidFooBar(thisHelper>helper,Expressi

c# - 带有 yield 返回的异步任务<IEnumerable>?

以下方法无法编译。替代方案?publicstaticasyncTask>GetRecordsAsync(thisTransactiontransaction,stringcommandText,paramsSqlParameter[]parameters){//GetaSqlDataReadervarreader=awaittransaction.GetReaderAsync(commandText,parameters);varfieldCount=-1;//Beginiteratingthroughrecordsasynchronouslywhile(awaitreader.Rea