Greetings
I’m looking for creating a extension method including a linq query.
What i’m looking for is either a method or extension method which can do something like this
var orderedList = OrderThisList<ModelName>(List<T> sourceList, //and something like m=>m.Username);
Where ModelName is the entity and Username is the field of which I want to order.
The method would look something like
public List<T> OrderThisList<T>(//some linq property?)
{
//What code goes here?
}
EDIT: It’s still extremely unclear what this question is all about, but it sounds like we’re dealing with in-memory collections rather than LINQ to SQL etc.
If the problem with using
OrderByis that it doesn’t sort the list in-place, you should be usingList<T>.Sort(), possibly passing in a custom comparator.My MiscUtil project has a few helper types which may help you. For example:
If you’re using LINQ to SQL and you’ve got a data context, you could use:
Now obviously that requires two type arguments, and you only want to specify one. There are various ways round this – basically you’d want to end up with a generic type and a method with one type argument (
TKey) which can be inferred. For example:It’s somewhat round-the-houses though considering that
OrderByis already available… could you explain why you want this, and also what LINQ provider is involved?