PROBLEM SOLVED!!!
The solution is Linq.Dynamic
You do it like this:
(from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year select c).OrderBy(order);
You have to download the System.Linq.Dynamic.dll and include it into your project.
Is there a way to order a linq query by the name of a field. like this:
from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c["ColName"] select c;
Or
from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c.GetType().GetField("ColName") select c;
None of these two works but I hope you know of a way to do this.
Firstly, set a string array. string[] values = { "tim", "amit", "tom", "jack", "saurav"}; Use the Sort() method to sort.
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.
In the LINQ-OrderBy method, it supports both query and method syntax. Let's see the query syntax with examples. OrderBy sorts the values of a collection in ascending or descending order. It sorts the collection in ascending order by default because ascending keyword is optional here.
In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element.
I spend to many time to resolve this issue, but didn't find any better than:
var queryExpression;
if (c["ColName"]=="CreateDate")
queryExpression.OrderBy(x => x.CreateDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With