C# Lists.ForEach 使用 LINQ/LAMBDA 选择标准
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14139402/
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
Lists.ForEach select with criteria by using LINQ/LAMBDA
提问by abc cba
I have List , I; only want to select base on certain criteria with LinQ/LAMBDA
我有 List ,我;只想使用 LinQ/LAMBDA 根据某些标准进行选择
My Code is
我的代码是
Lists.ForEach(x => x.IsAnimal == false { /* Do Something */ } );
I am getting error "only assignment, call, increment, decrement and new object expressions can be used as a statement" in this part x.IsAnimal == false
我在这部分收到错误“只有赋值、调用、递增、递减和新对象表达式可以用作语句” x.IsAnimal == false
i know we can achieve this easily with a for loop but I want to learn more by using LinQ/LAMBDA
我知道我们可以使用 for 循环轻松实现这一点,但我想通过使用 LinQ/LAMBDA 了解更多信息
回答by krajew4
Just use Whereand ToListbefore using ForEach
在使用 ForEach 之前只需使用Where和ToList
Lists.Where(x => !x.IsAnimal).ToList().ForEach(...)
回答by O. R. Mapper
Please read up on the syntax of lambda expressions: A lambda expression represents a method; the part before the =>
is the list of parameters, and the part afterwards is either a single expression whose result will be returned, or a method body.
请仔细阅读lambda 表达式的语法: 一个 lambda 表达式代表一个方法;前面的部分=>
是参数列表,后面的部分要么是将返回结果的单个表达式,要么是方法体。
You can add your restriction within that method body:
您可以在该方法主体中添加您的限制:
Lists.ForEach(x => {
if (!x.IsAnimal) {
// do something
}
});
回答by LeffeBrune
Should be something like this:
应该是这样的:
things.Where(x => !x.IsAnimal).ToList().ForEach(x => { // do something });
I can see people are complaining about having to build new list to use ForEach. You could do the same thing with Select and stick to IEnumerable:
我可以看到人们抱怨必须构建新列表才能使用 ForEach。你可以用 Select 做同样的事情并坚持使用 IEnumerable:
things.Where(x => !x.IsAnimal).Select(x =>
{
// do something with it
return x; // or transform into something else.
});
回答by Lews Therin
That's not working because you can't have false{}
construct.
这是行不通的,因为你不能有false{}
构造。
Lists.ForEach(x => {
if(x.IsAnimal){
//Do Something
}
} );
回答by rickvdbosch
Please keep in mind that there isa difference between lists.foreach and a normal foreach. A 'normal' for each uses an enumerator, making it illegal to change the list that is iterated. the lists.foreach() does not use an enumerator (if I'm not mistaking it uses an indexer on the background) making it possible to change items in the array as you go.
请记住,lists.foreach 和普通的 foreach 之间是有区别的。每个的“正常”使用枚举器,因此更改迭代的列表是非法的。list.foreach() 不使用枚举器(如果我没有误会它在后台使用索引器),可以随时更改数组中的项目。
Hope this helps
希望这可以帮助
回答by Marcin Buciora
Try such code:
试试这样的代码:
class A {
public bool IsAnimal { get; set; }
public bool Found { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<A> lst = new List<A>();
lst.Add(new A() { IsAnimal = false, Found = false });
lst.Add(new A() { IsAnimal = true, Found = false });
lst.Add(new A() { IsAnimal = true, Found = false });
lst.Add(new A() { IsAnimal = false, Found = false });
// Here you first iterate to find only items with (IsAnimal == false)
// then from IEnumerable we need to get a List of items subset
// Last action is to execute ForEach on the subset where you can put your actions...
lst.Where(x => x.IsAnimal == false).ToList().ForEach(y => y.Found = true);
}
}
回答by kara
If you want to use "ForEach" on different types of collections you should write an extension for IEnumerable:
如果你想在不同类型的集合上使用“ForEach”,你应该为 IEnumerable 编写一个扩展:
public static class IEnumerableExtension
{
public static void ForEach<T>(this IEnumerable<T> data, Action<T> action)
{
foreach (T d in data)
{
action(d);
}
}
}
With this extension you can filter before using ForEach and you don't have to form a new list with the filtered items:
使用此扩展程序,您可以在使用 ForEach 之前进行过滤,而不必使用过滤后的项目形成新列表:
lists.Where(x => !x.IsAnimal).ForEach( /* Do Something */ );
I prefere the standard version:
我更喜欢标准版本:
foreach (var x in lists.Where(x => !x.IsAnimal)) { /* Do Something */ }
Not very long and clearly a loop with sideeffects.
不是很长,显然是一个带有副作用的循环。