tags:

views:

183

answers:

3

Instead of calling:

var shows = _repository.ListShows("PublishDate");

to return a collection of objects sorted by the publish date, I would like to use a syntax like this:

var shows = _repository.ListShows(s => s.PublishDate);

What do I need to write to take advantage of the lambda as an argument?

+5  A: 
public IEnumerable<Show> ListShows(Func<Show, string> stringFromShow)
{

}

Within that method, use

string str = stringFromShow(show);
Daniel Earwicker
+1  A: 
var shows = _repository.OrderBy(s=>s.PublishDate);
Jason Coyne
+1  A: 

Your ListShows method in your repository should look like this:

public static IEnumerable<Show> ListShows(Comparison<Show> comparison)
{
    List<Show> shows = new List<Show>();
    ... code here ...
    shows.Sort(comparison);
    return shows;
}

Then you can use a lambda to do the following (it's not as simple as your example, but it works):

ListShows((first, second) => first.PublishDate.CompareTo(second.PublishDate));
Michael Meadows