quite easy question about linq

T

Tony Johansson

Hello!

I have this linq query below which works as expected but I have one
question.
If I just change the .Select operator to *.Select(eo => new {id = e.id,
optionsCount = eo.optionsCount}));*
Here as you can see I have taken the id from variable e which is employee
insted of eo will the result be the same ?
I'm almost sure it will but I just want to be 100 % sure.


Employee[] employees = Employee.GetEmployeesArray();
EmployeeOptionsEntry[] empOptions =
EmployeeOptionEntry.GetEmployeeOptionEntries();
var employeeOptions = employees
.SelectMany(e => empOptions
.Where(eo => eo.id == e.id)
.Select(eo => new {id = eo.id, optionsCount
= eo.optionsCount}));

foreach (var item in employeeOptions)
Console.WriteLine(item);

//Tony
 
A

Anthony Jones

Tony Johansson said:
Hello!

I have this linq query below which works as expected but I have one
question.
If I just change the .Select operator to *.Select(eo => new {id = e.id,
optionsCount = eo.optionsCount}));*
Here as you can see I have taken the id from variable e which is employee
insted of eo will the result be the same ?
I'm almost sure it will but I just want to be 100 % sure.


Employee[] employees = Employee.GetEmployeesArray();
EmployeeOptionsEntry[] empOptions =
EmployeeOptionEntry.GetEmployeeOptionEntries();
var employeeOptions = employees
.SelectMany(e => empOptions
.Where(eo => eo.id == e.id)
.Select(eo => new {id = eo.id,
optionsCount
= eo.optionsCount}));

foreach (var item in employeeOptions)
Console.WriteLine(item);

Assuming that the id is something simple like an int or string then the
answer is yes.

However if the type of the id property is something more complex then it
would depend on how the == operator is implemented by that type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top