C# 用 linq 替换 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17572213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:52:55  来源:igfitidea点击:

Replace foreach loop with linq

c#linq

提问by TOP KEK

I tried to replace code

我试图替换代码

foreach (var discovery in mpwrapper.parser.Discoveries)
{
   solution.AddFile("Discoveries", discovery.DisplayStringName + ".mpx", discovery);
}

with the following linq expression

使用以下 linq 表达式

mpwrapper.parser.Discoveries.Select(
                    s => solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s));

But got an error

但是出错了

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

无法从用法推断方法“System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)”的类型参数。尝试明确指定类型参数。

How to convert this foreach loop to linq query where I execute a method on each object in my IEnumerable collection?

如何将此 foreach 循环转换为 linq 查询,在其中我对 IEnumerable 集合中的每个对象执行一个方法?

采纳答案by dotixx

I think what you need is the ForEach method ;)

我认为您需要的是 ForEach 方法;)

mpwrapper.parser.Discoveries.ToList().ForEach(s => { solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s); });

回答by user2509738

The problem is Select needs to return a value, but also Linq is not designed to mutate collections. Think of select as a transformation over the collection rather than a state change for each item

问题是 Select 需要返回一个值,而且 Linq 也不是为了改变集合而设计的。将选择视为对集合的转换,而不是每个项目的状态更改

Maybe a foreach loop is the best solution here

也许 foreach 循环是这里最好的解决方案

回答by Marko Juvan?i?

If Discoveries is a list, then do it like this

如果 Discoveries 是一个列表,那么就这样做

mpwrapper.parser.Discoveries.ForEach(discovery => solution.AddFile("Discoveries", discovery .DisplayStringName + ".mpx", discovery);

If not, convert it to List first :)

如果没有,请先将其转换为 List :)

回答by sthiers

The List<T>.ForEachmethod does the trick.

List<T>.ForEach方法可以解决问题。

However, this method does not exist on IEnumerable<T>.

但是,该方法在 上不存在IEnumerable<T>

回答by Alex Filipovici

Try this:

尝试这个:

mpwrapper.parser.Discoveries.ToList()
    .ForEach(s =>
        solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s));

回答by NPSF3000

LINQ stands for Language INtegrated Query... yet you aren't actually queryinganything.

LINQ 代表 Language INtegrated Query...但您实际上并没有查询任何内容。

If mpwrapper.parser.Discoveriesis a List<T>, you can use the ForEachmethod; or, if it is an IEnumerable, you could always add a ForEachextension method… but this is more a minor aesthetic change and has nothing to do with LINQ.

如果mpwrapper.parser.Discoveries是 a List<T>,则可以使用该ForEach方法;或者,如果它是IEnumerable,你总是可以添加一个ForEach扩展方法……但这更像是一个微小的美学变化,与 LINQ 无关。

回答by Leon van Wyk

I use a little cheat with the .All method. It simply requires a return boolean value and presents very neatly. I included a sample with embeded linq inside the .All

我在 .All 方法中使用了一个小技巧。它只需要一个返回布尔值并且非常整洁地呈现。我在 .All 文件中包含了一个带有嵌入式 linq 的示例

configurations.All(c =>
{
    var gcx = globalConfigurations.FirstOrDefault(gc =>
        gc.Type == c.Type && configurationGuids.Any(cGuid => gc.Guid == cGuid)
    );
    return true;
});