Lambda Expressions and Methods

Hi I have a collection of objects in Listview and I need to know if I can pass through them using a lambda expression. and call the method in the expression in it.

Suppose I need to save a group of people in a database.

List<People> someList;
someList.Select(person => person.Save());

can this be done? so far I have not been able to get it to work. thank

+3
source share
3 answers

You can use the ForEach method for a generic list:

List<People> someList;
someList.ForEach(person => person.Save());
+9
source
someList.ForEach(p => p.Save());
+4
source

, foreach:

foreach(People p in someList)
{
    p.Save();
}

- LINQ, , .Select(...) IEnumerable/IQueryable, , -.

, , , IEnumerable/IQueryable. , :

someList.Select(person => person.Save()).Count();

, Save() -void.

: , List < > , :

someList.ForEach(person => person.Save());
+3

Source: https://habr.com/ru/post/1796613/


All Articles