Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do a linq query on an object array?

Tags:

c#

linq

I have a collection of type objects which I know they are type Employee. I'd like to perform some linq operations on them using the employee some thing similar to this:

 var options = (from e in m_Employees  
                      select (e as Employee).DepartmentCode).Distinct();

But the as employee is not surprisingly giving an error. is there a way around it?

Changing the collection is not really an options since I,m maintaining the code and I want to avoid big changes.

like image 391
Marcom Avatar asked Apr 17 '26 05:04

Marcom


2 Answers

You can use either

from e in m_Employees.Cast<Employee>()
    select e.DepartmentCode

or

from e in m_Employees.OfType<Employee>()
    select e.DepartmentCode

Cast thows an error if you can not cast each item to Employee, but OfType will filter out those objects not matching the type.

like image 196
Albin Sunnanbo Avatar answered Apr 18 '26 18:04

Albin Sunnanbo


Rather than using as Employee it would be better to make the compiler basically insert a call to Cast<T> using an explicitly typed range variable:

var options = (from Employee e in m_Employees
               select e.DepartmentCode).Distinct();

Or alternatively and equivalently:

var options = m_Employees.Cast<Employee>()
                         .Select(e => e.DepartmentCode)
                         .Disinct();

However, I still wouldn't have expected your original code to fail, if the array really does only include Employee references... If you were getting a NullReferenceException then either one of the values was null, or it was a non-null reference to a non-Employee object. These will both still give you an error with the above code, but you'll be able to see which one based on whether you still get a NullReferenceException or an InvalidCastException.

In general you should only use as when you're going to use the result conditionally. If you're sure that every value is really of the right type, you should use a cast instead - so that if you're wrong, you'll get the code blowing up with an exception instead of propagating a null reference to the rest of the code, where it could cause harm later on and make it hard to spot the source of the error.

If you were getting a compile-time error then there are a number of possible causes, based on what exception you were seeing.

EDIT: Okay, so it was an IEnumerable causing a compile-time error... Cast<T>() and OfType<T>() are both extension methods on just IEnumerable instead of on IEnumerable<T>.

like image 24
Jon Skeet Avatar answered Apr 18 '26 20:04

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!