相关疑难解决方法(0)

LINQ相当于foren for IEnumerable <T>

我想在LINQ中做相同的以下内容,但我无法弄清楚如何:

IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
Run Code Online (Sandbox Code Playgroud)

什么是真正的语法?

linq ienumerable foreach

694
推荐指数
12
解决办法
46万
查看次数

为什么IList <T>上的.ForEach()而不是IEnumerable <T>?

可能重复:
为什么IEnumerable接口上没有ForEach扩展方法?

我注意到在编写LINQ-y代码时,.ForEach()是一个很好用的习惯用法.例如,这是一段代码,它接受以下输入,并产生这些输出:

{ "One" } => "One"
{ "One", "Two" } => "One, Two"
{ "One", "Two", "Three", "Four" } => "One, Two, Three and Four";
Run Code Online (Sandbox Code Playgroud)

和代码:

private string InsertCommasAttempt(IEnumerable<string> words)
{
    List<string> wordList = words.ToList();
    StringBuilder sb = new StringBuilder();
    var wordsAndSeparators = wordList.Select((string word, int pos) =>
        {
            if (pos == 0) return new { Word = word, Leading = string.Empty };
            if (pos == wordList.Count - 1) return new { Word = word, Leading = " …
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable list c#-3.0

52
推荐指数
3
解决办法
3万
查看次数

标签 统计

ienumerable ×2

linq ×2

c# ×1

c#-3.0 ×1

foreach ×1

list ×1