Sorting Anonymous Types
Posted
by strobaek
on Stack Overflow
See other posts from Stack Overflow
or by strobaek
Published on 2010-04-07T07:22:31Z
Indexed on
2010/04/07
7:33 UTC
Read the original article
Hit count: 593
c#
|linq-to-sql
I have a question of how to sort an anonymous type.
Using Linq2SQL I have the following query, which returns a list of submissions:
var submissions = EventSubmissions
.Where(s => s.EventId.Equals(eventId));
Consider the following interface (somewhat simplyfied):
public interface IQuerySorter
{
IOrderedQueryable Sort(IQueryable query);
IOrderedQueryable<T> Sort<T, U>(IQueryable<T> query, Expression<Func<T,U>> selector);
...
}
Using this interface allows me to implement a number of 'sorters', e.g. on Date, Rating or whether or not a submission has been nominated (for voting).
sortedQuery = sorter.Sort(submissions)
So far so good. A submission can be made "votable". I get the number of votes a nominated submission may have using the following query:
var withVoteCount = submissions
.Select(s => new {NumberOfVotes = s.Votes.Count(), Submission = s});
I would like to sort this new query by NumberOfVotes using my "general" sorter class, but run into the problem that the anonymous type/member does not seem to live outside the repository-method, hence I am unable to sort on it.
Any input would be greatly appreciated.
© Stack Overflow or respective owner