Return a new object using the ForEach function

0

Have the following list List<EmployeeTotal> totals = Context.Database.SqlQuery<EmployeeTotal>(query).ToList(); I want to transform all objects from the totals list into a new type list        List<EmployeeTotalAdapter> totalsAdapter; for this conversion I use the constructor of the EmployeeTotalAdapter class to get an object of type EmployeeTotal

I was thinking of using foreach with lambda to add and generate conversions

  List<EmployeeTotalAdapter> totalsAdapter = new List<EmployeeTotalAdapter>(); 
  totalsAdapter.Add(totals.ForEach(t => new EmployeeTotalAdapter(t)));

But it does not work; Argument1: can not convert from void to EmployeeTotalAdapter

Is it possible to do something like this?

    
asked by anonymous 13.06.2017 / 18:26

1 answer

3

The instruction is reversed.

The right thing would be:

  

for each element of totals , name t . Add a new element in totalsAdapter .

totals.ForEach(t => totalsAdapter.Add(new EmployeeTotalAdapter(t)));

You can also do something with LINQ

var totalsAdapter = totals.Select(t => new EmployeeTotalAdapter(t)).ToList();
    
13.06.2017 / 18:32