score:10
Enumerable.OrderByDescending
if the problem was that you wanted descending and not ascending
score:5
If you mean a non-generic IEnumerable
, you should use Cast
or OfType
to get an IEnumerable<T>
first, then you can use the normal OrderBy
/ OrderByDescending
calls.
For example:
IEnumerable test = new string[] { "abc", "x", "y", "def" };
IEnumerable<string> orderedByLength = test.Cast<string>()
.OrderBy(x => x.Length);
You can also do this by explicitly stating the type in a query expression:
IEnumerable<string> orderedByLength = from string x in test
orderby x.Length
select x;
EDIT: Now that the question has been clarified, the query expression form is:
var query = from value in collection
orderby value.SomeProperty descending
select value;
score:4
If your talking about a generic IEnumerable, below is a trimmed down example of usage.
// Using complex type
class Person()
{
public string Name;
}
IEnumerable<Person> myEnumerable = new List<Person>();
this.myEnumerable.OrderByDescending(person => person.Name)
// Using value type
IEnumerable<int> ints = new List<int>();
ints.OrderByDescending(x => x);
Source: stackoverflow.com
Related Articles
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- how to order asc/dsc with lambda or linq
- Join/Where with LINQ and Lambda
- Preserving order with LINQ
- Multiple Order By with LINQ
- LINQ Lambda Group By with Sum
- Preserve order of values with linq and ToLookup()
- Selecting multiple columns with linq query and lambda expression
- LINQ Order By Descending with Null Values on Bottom
- Translate SQL to lambda LINQ with GroupBy and Average
- How can anonymous types be created using LINQ with lambda syntax?
- Order by does not work with Concat() in LINQ
- C# linq expression in lambda with contains
- Entity Framework - Linq query with order by and group by
- Linq Query with SUM and ORDER BY
- Dynamic linq order by on nested property with null properties
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- Has anyone done the Linq 101 samples with Lambda syntax?
- convert linq to lambda with multiple joins
- Can we control LINQ expression order with Skip(), Take() and OrderBy()
- LINQ Queries with dynamic Order By
- Use Lambda or Linq to create Model of SelectList with Selected item for razor View in MVC5
- How to rewrite this LINQ using join with lambda expressions?
- Getting an invalid cast exception when trying to order a list of objects with linq
- Order by with Linq + Include
- Multiple Select and Join with LINQ and Lambda
- LINQ Lambda query 'select' not working with oData
- LINQ with Lambda expression - Join, Group By, Sum and Count
- Change the order of List items conditionally with LINQ
- Object grouping with linq or lambda expression
- lambda expression for multiple countries in Linq to Entity
- Dictionary<Foo, List<Bar>> How to assign the list to a property of the key with LINQ?
- Can LINQ based DataGridView's be "disconnected" from the LINQ expression?
- LINQ and DB can not retrieve correct data back
- How to include all elements of an array in a SQL query?
- Linq select all numbers that are in any interval from another list of intervals
- Linq where IN using method syntax
- Parameter "@Name" not found in the collection
- Lambda expressions query null field
- Add XElement only if value exists
- Universe Entity Framework very slow to select
- Entity Framework linq query for multiple children
- C# Linq GroupBy
- Selecting distinct records in a declarative linq data source?
- Logical Thinking: Using Dynamic vs Static Values to Represent Data
- ASP.NET Filter IQueryable with Custom Expression
- LINQ-to-List and IEnumerable issues
- How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum total
- LINQ search / match
- Linq select object from list depending on objects attribute