score:2
You could use reflection, but that would be rather slow. The most efficient would be declare a delegate to use in the sort, and assign a function depending on the string:
Func<Employee,string> order;
switch (field) {
case "FirstName": order = x => x.FristName;
case "LastName": order = x => x.LastName;
}
For the direction I think it's best to simply use separate codes:
var list = _employeeService.List();
IEnumerable<employee> sorted;
if (direction == "ascending") {
sorted = list.OrderBy(order);
} else {
sorted = list.OrderByDescending(order);
}
_model.List = sorted.ToList<Employee>();
score:1
Search for sites that talk about dynamic LINQ expressions. For example, this one shows how to do dynamic sorting:
http://blogs.sftsrc.com/stuart/archive/2009/02/19/130.aspx
You can then also choose whether to call OrderBy
or OrderByDescending
depending on the direction parameter.
Source: stackoverflow.com
Related Query
- Order IList with field name in argument
- A field or property with the name 'OrderID' was not found on the selected data source
- C# - code to order by a property using the property name as a string
- Call OrderBy() with a field name as a string
- .NET Core 3 InvalidOperationException on OrderBy with dynamic field name
- Expression predicates with field name as parameter
- Linq order by Area Name with one exception
- IList with an implicit sort order
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- What is the best way to order a list by numeric field and put objects with value of 0 at the bottom?
- select from a list with field name and field value with linq (similar to select command)
- Order by child records using dynamic field with entity framework
- How to order list of files by file name with number?
- How to get a variable replaced with a field name in a LINQ?
- How to specify a double order by expression using sub part of field name
- Order a list by a string value with the same name as the property
- Expression predicates with field name with a ToUpper
- Get MVC form field name with Html.FieldNameFor() defined by Orchard CMS
- Linq order by with a field to retrieve dynamically in vb.net
- Is there anyway to use field name with concatenated from the url?
- LINQ Queries with dynamic Order By - more than one field
- How to quickly match names (fname, lname) in different order with full name c#
- Preserving order with LINQ
- Multiple Order By with LINQ
- Calling generic method with a type argument known only at execution time
- LINQ OrderBy with more than one field
- What does this C# code with an "arrow" mean and how is it called?
- LINQ orderby on date field in descending order
- A specified Include path is not valid. The EntityType does not declare a navigation property with the name *
- Entity Framework with Linq, inner Join, Group By, Order By
More Query from same tag
- What function acts as .SelectMany() in jQuery?
- Get Database values based on linq query
- LINQ: Select all from each group except the first item
- Linq left outer join onto same list to find leaf nodes
- cannot cast expression of type 'system.xml.linq.xelement to type yyy
- How to convert rows into columns using LINQ?
- C# linq Files.ReadAllLines() fails for large 650MB CSV file
- Converting selector expression to predicate expression
- How do I create an expression tree calling IEnumerable<TSource>.Any(...)?
- C# MongoDB "Near" Query
- How can I get the minimum values and date of each group?
- Check for null before select on linq
- Joining two tables with LINQ while also returning null records from the second table
- Resolving method overload among LINQ query methods
- Get MethodInfo for Enumerable.First() vs MethodInfo for Enumerable.OfType()?
- WPF Linq Binding join-qry to DataGrid
- LINQ Query - Custom Output, Group Headers
- Linq query with datatable
- Two distinct aggregate queries combined: possible? (EF Core 5, SQL Server)
- "The parameter was not bound in the specified LINQ to Entities query expression." Specification Pattern And
- LINQ: Get unique rows by selected ID's based on latest time
- order by List of Guids position
- Count the number of Enum values in list using Linq
- Sorting Stored Procedure Results using Linq from another method
- How to create a new DateTime where condition using table field with LINQ
- Pivot data in linq
- Binding Grid to a Linq Datasource from Winforms (.net)
- Is there a way to loop through properties of a list of objects using linq?
- linq query to get the latest record with default if empty returning list
- LINQ - Dynamic Where Clause on "Query Syntax"