List <T> Order
I have a problem, I allow the user to select the criteria for ordering a list
Let's say my list is called
List<Cars> AllCars = new List<Cars>;
allCars = //call the database and get all the cars
Now I want to order this list
allCars.orderBy(registrationDate)
I understand that this does not work, but I do not have anyidea that I should put in brackets.
I understand that this does not work, but I do not have anyidea that I should put in brackets.
Announcement Enumerable.OrderBy-
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
and since this is an extension method, it can be called as
source.OrderBy(keySelector).
Yours List<Car>plays a role sourcelike List<T> : IEnumerable<T>. The second parameter is more interesting and one that you confuse. It is declared as type
Func<TSource, TKey>
, , TSource ( Car) TKey; , TKey. , Car.registrationDate, TKey is DateTime. , ?
DateTime GetRegistrationDate(Car car) {
return car.registrationDate;
}
OrderBy :
allCars.OrderBy(GetRegistrationDate).
# 2.0 ; , .
allCars.OrderBy(delegate(Car car) { return car.registrationDate; });
# 3.0 -,
allCars.OrderBy(car => car.registrationDate);
c => c.registrationDate - -, Func<Car, DateTime>, Enumerable.OrderBy.
allCars.orderBy(registrationDate)
, registrationDate . , - registrationDate . , Car.registrationDate , , ConferenceAttendee.registrationDate . , Car.registrationDate. , .