Using dynamic types with LINQ


What is the advantage of using dynamic type in the below code?

public static List<dynamic> GetEmployees()
    List<Employee> source = GenerateEmployeeCollection();
    var queyResult = from employee in source
                        where employee.Age > 20
                        select new { employee.FirstName, employee.Age };

    return queyResult.ToList<dynamic>();

And when would you go for returning a List.

Is to avoid creating Types which would be used only very rarely?

Using anonymous types is great if you're making a query and only you are consuming the results in your method.

In this case, you're returning the data from your method. When returning data from a method, you want the consumer to know exactly what to expect (which they obviously don't with a dynamic type). Therefore, using a dynamic type is certainly not a good idea.