I tried accordionSlides.OrderBy(m => ColumnName) it did not work?
Below code works. But I have a variable for ColumnName. It could be m.CustomerItemNumber or m.ItemNumber etc..
Please help with syntax.
public class Company
{
public string ItemGroupDesc { get; set; }
public int CustomerItemNumber { get; set; }
public int ItemNumber { get; set; }
public double Quantity { get; set; }
}
you may want to check the value in the Session variable - that you're using for columnname.
this works for me:
string SortColumnName = "EmployeeID";
NorthWindOrdersDataContext dc = new NorthWindOrdersDataContext();
var query = from o in dc.Orders
select new
{
EmployeeID = o.EmployeeID,
CustomerID = o.CustomerID
};
query = query.OrderBy(SortColumnName, true);
GridView2.DataSource = query;
GridView2.DataBind();
* to confirm: the static class should be a separate physical file
EDIT: also, try to sort on the result; use a variable for the results .
using System;
using System.Linq;
using System.Linq.Expressions;
public static class DynamicSortExtension
{
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
bool desc) where TEntity : class
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}
}
Thanks alot. How would I use this Static Class/ I have it in a seperate file.
And my Orderby is not accepting two parameters.
I think my query is of type IEnumerable. does that matter?
I get this error
Error 38 Instance argument: cannot convert from 'System.Linq.IOrderedEnumerable<AnonymousType#1>' to 'System.Linq.IQueryable'
Error 39 'System.Linq.IOrderedEnumerable<AnonymousType#1>' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable, string, params object[])' has some invalid arguments
Company[] accordionSlides = new
Company[pTable.Rows.Count];
var query = accordionSlides
.GroupBy(c => c.IM_ItemGroupDesc, c => new { CustomerItemNumber = c.CustomerItemNumber, ItemNumber = c.ItemNumber, Quantity = c.Quantity },
(k, enumerable) => new { Key = k, Values = enumerable });
query = query.OrderBy(ColumnName,false);
public class Company
{
public string ItemGroupDesc { get; set; }
public int CustomerItemNumber { get; set; }
public int ItemNumber { get; set; }
public double Quantity { get; set; }
}
urpalshu
Member
50 Points
254 Posts
Syntax for using OrderBy in Linq
Jun 02, 2010 08:37 PM|LINK
Hello
I would want to use a Variable column Name when I do OrderBy
accordionSlides.OrderBy(m => m.CustomerItemNumber)
please could somebody help with syntax.
I tried accordionSlides.OrderBy(m => ColumnName) it did not work?
Below code works. But I have a variable for ColumnName. It could be m.CustomerItemNumber or m.ItemNumber etc..
Please help with syntax.
public class Company { public string ItemGroupDesc { get; set; } public int CustomerItemNumber { get; set; } public int ItemNumber { get; set; } public double Quantity { get; set; } }MyAccordion.DataSource = accordionSlides.OrderBy(m => m.CustomerItemNumber) .GroupBy(c => c.IM_ItemGroupDesc, c => new { CustomerItemNumber = c.CustomerItemNumber, ItemNumber = c.ItemNumber, Quantity = c.Quantity }, (k, enumerable) => new { Key = k, Values = enumerable }); MyAccordion.DataBind();Thanks,
smithspd
Participant
1497 Points
463 Posts
Re: Syntax for using OrderBy in Linq
Jun 02, 2010 09:00 PM|LINK
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/4b2da6af-0d2b-4cc9-909d-ed38a237cef1
PeteNet
All-Star
81342 Points
11398 Posts
Re: Syntax for using OrderBy in Linq
Jun 02, 2010 09:00 PM|LINK
use an extension class, examples:
http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/06/05/composable-linq-to-sql-query-with-dynamic-orderby.aspx
you'd be able to use it like:
query = query.OrderBy("variableColName", false);
Peter
urpalshu
Member
50 Points
254 Posts
Re: Syntax for using OrderBy in Linq
Jun 03, 2010 02:35 PM|LINK
Thank you PeteNet
Somehow I am unable to use it correctly. I added the below class to my page.
public static class DynamicOrderBy { public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc) where TEntity : class { string command = desc ? "OrderByDescending" : "OrderBy"; var type = typeof(TEntity); var property = type.GetProperty(orderByProperty); var parameter = Expression.Parameter(type, "p"); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExpression = Expression.Lambda(propertyAccess, parameter); var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExpression)); return source.Provider.CreateQuery<TEntity>(resultExpression); } }PeteNet
All-Star
81342 Points
11398 Posts
Re: Syntax for using OrderBy in Linq
Jun 03, 2010 02:50 PM|LINK
you may want to check the value in the Session variable - that you're using for columnname.
this works for me:
string SortColumnName = "EmployeeID"; NorthWindOrdersDataContext dc = new NorthWindOrdersDataContext(); var query = from o in dc.Orders select new { EmployeeID = o.EmployeeID, CustomerID = o.CustomerID }; query = query.OrderBy(SortColumnName, true); GridView2.DataSource = query; GridView2.DataBind();* to confirm: the static class should be a separate physical file
EDIT: also, try to sort on the result; use a variable for the results .
Peter
urpalshu
Member
50 Points
254 Posts
Re: Syntax for using OrderBy in Linq
Jun 03, 2010 07:15 PM|LINK
Thanks, am I missing some using directive.
How would I use this
DynamicOrderBy class.
Also it accepts 3 paraments? But yours does not.
Thanks
PeteNet
All-Star
81342 Points
11398 Posts
Re: Syntax for using OrderBy in Linq
Jun 03, 2010 07:25 PM|LINK
here's the version I'm using:
using System; using System.Linq; using System.Linq.Expressions; public static class DynamicSortExtension { public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc) where TEntity : class { string command = desc ? "OrderByDescending" : "OrderBy"; var type = typeof(TEntity); var property = type.GetProperty(orderByProperty); var parameter = Expression.Parameter(type, "p"); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExpression = Expression.Lambda(propertyAccess, parameter); var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExpression)); return source.Provider.CreateQuery<TEntity>(resultExpression); } }Peter
urpalshu
Member
50 Points
254 Posts
Re: Syntax for using OrderBy in Linq
Jun 04, 2010 04:39 PM|LINK
Thanks alot. How would I use this Static Class/ I have it in a seperate file.
And my Orderby is not accepting two parameters.
I think my query is of type IEnumerable. does that matter?
I get this error
Error 38 Instance argument: cannot convert from 'System.Linq.IOrderedEnumerable<AnonymousType#1>' to 'System.Linq.IQueryable'
Error 39 'System.Linq.IOrderedEnumerable<AnonymousType#1>' does not contain a definition for 'OrderBy' and the best extension method overload 'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable, string, params object[])' has some invalid arguments
Company[] accordionSlides = new Company[pTable.Rows.Count];
var query = accordionSlides
.GroupBy(c => c.IM_ItemGroupDesc, c => new { CustomerItemNumber = c.CustomerItemNumber, ItemNumber = c.ItemNumber, Quantity = c.Quantity },
(k, enumerable) => new { Key = k, Values = enumerable });
query = query.OrderBy(ColumnName,false);
Thanks,
PeteNet
All-Star
81342 Points
11398 Posts
Re: Syntax for using OrderBy in Linq
Jun 07, 2010 08:28 AM|LINK
you could use the AsQueryable() method on IEnumerable (also on generic IEnumerable<T>) to convert to IQueryable
Peter
vikasrulez
Participant
776 Points
183 Posts
Re: Syntax for using OrderBy in Linq
Jun 07, 2010 10:31 AM|LINK
MyAccordion.DataSource = accordionSlides.GroupBy(c => c.IM_ItemGroupDesc, c => new { CustomerItemNumber = c.CustomerItemNumber, ItemNumber = c.ItemNumber, Quantity = c.Quantity },
(k, enumerable) => new { Key = k, Values = enumerable }).OrderBy(m => m.CustomerItemNumber);
MyAccordion.DataBind();