score:0
You can create dictionary with Func<string, IComparable>
mapping to properties of your class.
public class ItemWithProperty
{
public string Property { get; set; }
}
public static void Main()
{
Dictionary<string, Func<ItemWithProperty, IComparable>> stringPropertyMap = new Dictionary<string, Func<ItemWithProperty, IComparable>>()
{
{"param1", item => item.Property}
};
List<ItemWithProperty> toBeOrdered = new List<ItemWithProperty>();
string[] parameters = {"param1"};
var sorted = toBeOrdered.OrderBy(stringPropertyMap[parameters[0]]);
}
score:-1
You use this if you change your method signature:
private static IEnumerable<dynamic> ApplySort(IEnumerable<dynamic> listToBeSorted, ICollection<KeyValuePair<string, string>> sorters)
{
var orderBy = (sorters == null || sorters.Count == 0) ? new KeyValuePair<string, string>("createddate", "1") : sorters.First();
var thenBys = (sorters == null || sorters.Count == 1) ? new List<KeyValuePair<string, string>>() : sorters.Except(Enumerable.Repeat(orderBy, 1));
var orderedEnumerable = orderBy.Value == "1"
? listToBeSorted.OrderBy(x => GetPropertyValue(x, orderBy.Key))
: listToBeSorted.OrderByDescending(x => GetPropertyValue(x, orderBy.Key));
orderedEnumerable = thenBys.Aggregate(orderedEnumerable, (current, thenBy) => thenBy.Value == "1"
? current.ThenBy(x => GetPropertyValue(x, thenBy.Key))
: current.ThenByDescending(x => GetPropertyValue(x, thenBy.Key)));
return orderedEnumerable.ToList();
}
private static object GetPropertyValue(dynamic obj, string propName)
{
Type t = obj.GetType();
return t.GetProperty(propName).GetValue(obj, null);
}
Try :
static void Main(string[] args)
{
var list = new List<dynamic>();
list.Add(new { name = "Billy" });
list.Add(new { name = "Johnny" });
list.Add(new { name = "Ali" });
var list2 = ApplySort(list, new List<KeyValuePair<string, string>>(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("name", "1") }));
foreach (var o in list2)
{
Console.WriteLine(o.name);
}
Console.ReadLine();
}
Source: stackoverflow.com
Related Articles
- Dynamically cross-join multiple different-size collections together in Linq (C#)
- Does this LINQ code perform multiple lookups on the original data?
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ Source Code Available
- How to perform multiple Linq to Entities orderings dynamically
- multiple orderby in this linq code
- A linq statement with multiple group by columns not allowing a thenby
- Multiple Table Join in Linq C# Dynamically
- LINQ How to use multiple ThenBy dynamically?
- creating Linq to sqlite dbml from DbLinq source code
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- source code for LINQ 101 samples
- How to sort list on multiple properties in one line of code in Linq
- LINQ Multiple Sort Dynamically C#
- dynamically append multiple linq expressions at run-time
- How to dynamically create linq code at runtime and get results
- How to add multiple items in a list dynamically using linq in c#
- c# Linq or code to extract groups from a single list of source data
- How do I minimize C#, LINQ code in Deleting Multiple Records
- Multiple "order by" in LINQ
- C# Linq Group By on multiple columns
- Convert string[] to int[] in one line of code using LINQ
- How to do joins in LINQ on multiple fields in single join
- Multiple Order By with LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Entity framework linq query Include() multiple children entities
- EF LINQ include multiple and nested entities
- Linq to Sql: Multiple left outer joins
- Select multiple records based on list of Id's with linq
- Select Multiple Fields from List in Linq
- Simple Example Subquery Linq
- Group by Linq vs Transact Sql
- Method result in groupby
- ASP.NET MVC EntityFramework: how to use LINQ inside ActionResult controller method?
- LINQ SQL Trying to merge 3 recordsets into one
- linq predicate remove objects from collection issue
- Get DropDownListFor to display name of each model object
- Reverse Engineering a LINQ Statement
- Why do LINQ operations lose the static type of the collection?
- Select String that is not a property of another object
- Select a Dictionary<T1, T2> with LINQ
- How to sort a lookup?
- EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context
- C# - Comparing a list<t> to a dynamic list
- Linq most efficient method
- Printing useful output with Log4net in Application_Error and LINQ queries
- Interface than allow a function to return an IEnumerable<TD> or IQueryable<TD> based on the query
- Casting String as DateTime in LINQ
- Generic List firstOrDefault
- Finding the maximum value in a list