Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lists.ForEach select with criteria by using LINQ/LAMBDA

Tags:

c#

linq

I have List , I; only want to select base on certain criteria with 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

i know we can achieve this easily with a for loop but I want to learn more by using LinQ/LAMBDA

like image 482
abc cba Avatar asked Jan 03 '13 12:01

abc cba


2 Answers

Just use Where and ToList before using ForEach

Lists.Where(x => !x.IsAnimal).ToList().ForEach(...)
like image 200
krajew4 Avatar answered Oct 21 '22 05:10

krajew4


That's not working because you can't have false{} construct.

Lists.ForEach(x => { 
                  if(x.IsAnimal){
                       //Do Something
                  }
             } );
like image 25
Lews Therin Avatar answered Oct 21 '22 06:10

Lews Therin