Tuesday, July 02, 2013

LINQ 10- Projection/Select- 2

IEnumerable’s extension method Select, automatically identifies the input and output type. Input and output types are not restricted.

So Select method is powerful enough to transform one object to another. We already have seen one example in  “LINQ 10- Projection/Select- 1” which transforms an integer array to string Enumerable.


Following statement extracts the Name from Employee object.

IEnumerable<String> selected = employees.Select(employee => empoyee.Name);


Here Employees is the source IEnumerable witch generic type argument Employee. So TSource is Employee.

Transformation function gets the Name from Employee and returns it. So TResult is String.


Even we can transform a class object to another class object

IEnumerable<Manager> selected = employees.Select(employee => new Manager() {
    ID = employee.ID,
    Name =  employee.Name


Above statement transforms a list of Employee objects to List of Manager Objects

No comments:

Post a Comment