score:2
Accepted answer
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 Articles
- 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
- IQueryable/Linq Orderby Fail: Sorts only a limited number of records
- Can this be done in 1 iteration instead of 2 and will it solve multiple enumeration issue?
- LINQ query IN for array
- Get Distinct List from csv file based on first column
- Why is one character missing in the query result?
- Linq convert a Method Syntax to Query Expression
- How to modify an item in an IEnumerable collection and retain its order
- C#: Remove duplicate values from dictionary?
- Only include in where condition if date is not low date(01/01/0001)
- How to remove duplicates from an Array using LINQ
- Multiple condition checks in the query?
- How can I query by property type rather than type name using LINQ?
- Simplest LINQ many-to-many solution
- querying nested array elements in C#
- How should I get started learning about ADO.NET Entity Framework?
- C# LINQ Translate Extension to Query syntax
- How to use LINQ to select object with minimum or maximum property value
- Linq to Dynamics - compare two properties in where clause
- LINQ join issue, returns all null value
- Merging n Dictionary instances & execute action on duplicate keys