Return a new object using the Foreach function

Asked

Viewed 192 times

0

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

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

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

However it does not work informs; Argument1: cannot Convert from void to Employeetotaladapter

it is possible to do something like this ?

1 answer

3


The instruction is backwards.

The right thing would be:

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

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

It is also possible to do something with LINQ

var totalsAdapter = totals.Select(t => new EmployeeTotalAdapter(t)).ToList();
  • I was told that the order of the factors did not alter the result hehehe, so release I accept the reply thank you very much

  • hahaha in this case makes a difference yes. Quiet xd

Browser other questions tagged

You are not signed in. Login or sign up in order to post.