2

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?

3 Answers 3

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

}

Within that method, use

string str = stringFromShow(show);
Sign up to request clarification or add additional context in comments.

Comments

1
var shows = _repository.OrderBy(s=>s.PublishDate);

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.