score:2
use a func delegate to store your ordering then pass that to the orderby method:
func<int, int> orderfunc = i => i; // func for ordering
var list = enumerable.range(1,10).orderbydescending(i => i); // 10, 9 ... 1
var newlist = list.orderby(orderfunc); // 1, 2 ... 10
as another example consider a person
class:
public class person
{
public int id { get; set; }
public string name { get; set; }
}
now you want to preserve a sort order that sorts by the name
property. in this case the func
operates on a person
type (t
) and the tresult
will be a string since name
is a string and is what you are sorting by.
func<person, string> nameorder = p => p.name;
var list = new list<person>
{
new person { id = 1, name = "abc" },
new person { id = 2, name = "def" },
new person { id = 3, name = "ghi" },
};
// descending order by name
foreach (var p in list.orderbydescending(nameorder))
console.writeline(p.id + ":" + p.name);
// 3:ghi
// 2:def
// 1:abc
// re-assinging the list
list = new list<person>
{
new person { id = 23, name = "foo" },
new person { id = 14, name = "buzz" },
new person { id = 50, name = "bar" },
};
// reusing the order function (ascending by name in this case)
foreach (var p in list.orderby(nameorder))
console.writeline(p.id + ":" + p.name);
// 50:bar
// 14:buzz
// 23:foo
edit: be sure to add tolist()
after the orderby
calls if you need a list<t>
since the linq methods will return an ienumerable<t>
.
score:1
calling tolist()
or toarray()
on your ienumerable<t>
will cause it to be immediately evaluated. you can then assign the resulting list or array to "save" your ordered list.
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- multiple orderby in this linq code
- LINQ Lambda efficiency of code groupby orderby
- LINQ to SQL: orderby a sum operation
- creating Linq to sqlite dbml from DbLinq source code
- Linq - 'Saving' OrderBy operation (c#)
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>
- LINQ Orderby Descending Query
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ OrderBy versus ThenBy
- Linq code to select one item
- How do I specify the Linq OrderBy argument dynamically?
- LINQ OrderBy with more than one field
- How are people unit testing code that uses Linq to SQL
- Linq OrderBy against specific values
- LINQ OrderBy is not sorting correctly
- Custom sort logic in OrderBy using LINQ
- LINQ orderby on date field in descending order
- C# LINQ Orderby - How does true/false affect orderby?
- Use own IComparer<T> with Linq OrderBy
- C# Linq OrderBy filtering null or empty values to be last
- How to OrderBy an integer in a string field in a Linq query
- LINQ OrderBy not ordering .. changing nothing .. why?
- Linq syntax for OrderBy with custom Comparer<T>
- How to apply multiple orderby in linq query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Why does a Linq Cast<T> operation fail when I have an implicit cast defined?
More Query from same tag
- Insert Anonymous object in a list of same anonymous type
- Reusable LINQ query except for where clause
- Simple Form Query
- IQueryable with String.Format and PatIndex
- Convert flat collection to one-?many C#/Linq
- LINQ to SQL and nested EntitySet type
- How to perform join using an expression
- Query a byte property with nhibernate cause invalid cast error
- How properly work with LINQ to XML?
- Convert an unit -> unit to Action
- How to Insert a New Node Using LINQ to XML when only xml data is available?
- LINQ where clause using Generic IQueryable source
- Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)
- Build a query using dynamic where condition and a dynamic list
- sql tables to xml
- Flatten a query of multiple records to one object
- C# - LINQ - shortest distance by GPS latitude and longitude
- How to Create a search function with nhibernate, linq?
- How can I compare two lists of objects with ingorance of order?
- Building 'flat' rather than 'tree' LINQ expressions
- Lambda Expression Group by
- Convert List<string> to List<object> in C#
- Linq Expression: Perform Distinct on a list dynamic property
- Function application in LINQ expressions
- C# LINQ - Union() the Group()-results
- Return error message from a void method in C#
- Linq to Entities Where Or clause
- Small Linq expression doesnt work
- How do I group data in an ASP.NET MVC View?
- Static expression as instance-property for dynamic linq-queries