<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Linq to Sql: Dynamic Sorting without using Complete Dynamic Linq Libraries</title>
	<atom:link href="http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/feed/" rel="self" type="application/rss+xml" />
	<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/</link>
	<description>The more you know, you know how little you know</description>
	<lastBuildDate>Thu, 10 Sep 2009 22:05:02 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: DoYouKnow.IN</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-51</link>
		<dc:creator>DoYouKnow.IN</dc:creator>
		<pubDate>Fri, 21 Aug 2009 07:51:18 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-51</guid>
		<description>Hi 

I want to do sorting of record from higher number of repeating in particular list...

any one suggest how to do that?</description>
		<content:encoded><![CDATA[<p>Hi </p>
<p>I want to do sorting of record from higher number of repeating in particular list&#8230;</p>
<p>any one suggest how to do that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart Thompson</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-44</link>
		<dc:creator>Stuart Thompson</dc:creator>
		<pubDate>Mon, 23 Feb 2009 18:50:09 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-44</guid>
		<description>This solution would evaluate your list and is more specific to sorting any collection than just with LINQ to SQL, but might also be a valuable addition to your toolbox in this space.  &lt;a href=&quot;http://blogs.sftsrc.com/stuart/archive/2009/02/19/130.aspx&quot; rel=&quot;nofollow&quot;&gt;http://blogs.sftsrc.com/stuart/archive/2009/02/19/130.aspx&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>This solution would evaluate your list and is more specific to sorting any collection than just with LINQ to SQL, but might also be a valuable addition to your toolbox in this space.  <a href="http://blogs.sftsrc.com/stuart/archive/2009/02/19/130.aspx" rel="nofollow">http://blogs.sftsrc.com/stuart/archive/2009/02/19/130.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Haitham A. El Refaie</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-43</link>
		<dc:creator>Haitham A. El Refaie</dc:creator>
		<pubDate>Thu, 12 Feb 2009 01:47:39 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-43</guid>
		<description>thanks a lot for this code it really helped me a lot,
but I suggest the following modification to improve it.
Why we need to modify:
    in case of linking to datagrid at first built of the page the sort expression is an empty string so the property at line 27 of the code will be null which will cause error on line 29, the solution will be on of the following:
1- validate the sortexpression before the calling of the sort function and give it initial value if it&#039;s empty.
2- to update or extension method to retun the same query if the sort expression is empty which is better, and it&#039;ll be like this: 

  public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class
        {
            if (!string.IsNullOrEmpty(sortExpression))
            {
                  //our function body goes here
             }
            else
            {
                return source;
            }
         }</description>
		<content:encoded><![CDATA[<p>thanks a lot for this code it really helped me a lot,<br />
but I suggest the following modification to improve it.<br />
Why we need to modify:<br />
    in case of linking to datagrid at first built of the page the sort expression is an empty string so the property at line 27 of the code will be null which will cause error on line 29, the solution will be on of the following:<br />
1- validate the sortexpression before the calling of the sort function and give it initial value if it&#8217;s empty.<br />
2- to update or extension method to retun the same query if the sort expression is empty which is better, and it&#8217;ll be like this: </p>
<p>  public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class<br />
        {<br />
            if (!string.IsNullOrEmpty(sortExpression))<br />
            {<br />
                  //our function body goes here<br />
             }<br />
            else<br />
            {<br />
                return source;<br />
            }<br />
         }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leather</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-34</link>
		<dc:creator>Leather</dc:creator>
		<pubDate>Thu, 25 Sep 2008 14:37:37 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-34</guid>
		<description>Sorry - the post seems to have removed all of my generic  references from the code above...</description>
		<content:encoded><![CDATA[<p>Sorry &#8211; the post seems to have removed all of my generic  references from the code above&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leather</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-33</link>
		<dc:creator>Leather</dc:creator>
		<pubDate>Thu, 25 Sep 2008 14:35:51 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-33</guid>
		<description>Great post - so thanks! I looked at various other attempts on the web and they all seemed to fail on certain field types etc. Anyway I tweaked this slightly to suit my own needs and came out with this:

&lt;code&gt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Linq;
using System.Text;

namespace LinqSorter
{
    public static class GenericSorter
    {
        public enum SortDirection
        {
            Ascending,
            Descending
        }

        public static IQueryable OrderBy(this IQueryable source, string fieldName) where TEntity : class
        {
            return OrderBy(source, fieldName, SortDirection.Ascending);
        }
        
        public static  IQueryable OrderBy(this IQueryable source, string fieldName, SortDirection sortDirection) where TEntity : class  
        {
            const string ORDER_BY_METHOD_NAME = &quot;OrderBy&quot;;
            const string ORDER_BY_DESCENDING_METHOD_NAME = &quot;OrderByDescending&quot;;
            const string PARAMETER_NAME = &quot;Entity&quot;;

            // Get the type of the entity being sorted.
            var type = typeof(TEntity); 

            // Create a parameter to pass into the Lambda expression (Entity =&gt; Entity.OrderByField).
            var parameter = Expression.Parameter(type, PARAMETER_NAME);

            // Get a reference to the type of the property being sorted.
            var property = type.GetProperty(fieldName);

            // Get a reference to the properties access member ( Entity.OrderByField ).
            var propertyAccess = Expression.MakeMemberAccess(parameter, property); 

            // Create the order by expression.
            var orderByExp = Expression.Lambda(propertyAccess, parameter); 

            string methodName;

            // Determine the method to actually call on the IQueryable interface.
            if(sortDirection == SortDirection.Ascending)
                methodName = ORDER_BY_METHOD_NAME;
            else
                methodName = ORDER_BY_DESCENDING_METHOD_NAME;

            // Get a reference to the method call.
            MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName,
                                                             new Type[] { type, property.PropertyType },  
                                                             source.Expression, Expression.Quote(orderByExp));  

            // Now apply the sort.
            return source.Provider.CreateQuery(resultExp);
        }
    }
&lt;/code&gt;

This can then be called on an IQueryable object as follows (I am using a basic repository here):

&lt;code&gt;

Repository r = new Repository();
            var sortedOrderDetails = r.GetEntities().OrderBy(&quot;DateShipped&quot;, GenericSorter.SortDirection.Descending);

&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Great post &#8211; so thanks! I looked at various other attempts on the web and they all seemed to fail on certain field types etc. Anyway I tweaked this slightly to suit my own needs and came out with this:</p>
<p><code><br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Linq.Expressions;<br />
using System.Data.Linq;<br />
using System.Text;</p>
<p>namespace LinqSorter<br />
{<br />
    public static class GenericSorter<br />
    {<br />
        public enum SortDirection<br />
        {<br />
            Ascending,<br />
            Descending<br />
        }</p>
<p>        public static IQueryable OrderBy(this IQueryable source, string fieldName) where TEntity : class<br />
        {<br />
            return OrderBy(source, fieldName, SortDirection.Ascending);<br />
        }</p>
<p>        public static  IQueryable OrderBy(this IQueryable source, string fieldName, SortDirection sortDirection) where TEntity : class<br />
        {<br />
            const string ORDER_BY_METHOD_NAME = "OrderBy";<br />
            const string ORDER_BY_DESCENDING_METHOD_NAME = "OrderByDescending";<br />
            const string PARAMETER_NAME = "Entity";</p>
<p>            // Get the type of the entity being sorted.<br />
            var type = typeof(TEntity); </p>
<p>            // Create a parameter to pass into the Lambda expression (Entity =&gt; Entity.OrderByField).<br />
            var parameter = Expression.Parameter(type, PARAMETER_NAME);</p>
<p>            // Get a reference to the type of the property being sorted.<br />
            var property = type.GetProperty(fieldName);</p>
<p>            // Get a reference to the properties access member ( Entity.OrderByField ).<br />
            var propertyAccess = Expression.MakeMemberAccess(parameter, property); </p>
<p>            // Create the order by expression.<br />
            var orderByExp = Expression.Lambda(propertyAccess, parameter); </p>
<p>            string methodName;</p>
<p>            // Determine the method to actually call on the IQueryable interface.<br />
            if(sortDirection == SortDirection.Ascending)<br />
                methodName = ORDER_BY_METHOD_NAME;<br />
            else<br />
                methodName = ORDER_BY_DESCENDING_METHOD_NAME;</p>
<p>            // Get a reference to the method call.<br />
            MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName,<br />
                                                             new Type[] { type, property.PropertyType },<br />
                                                             source.Expression, Expression.Quote(orderByExp));  </p>
<p>            // Now apply the sort.<br />
            return source.Provider.CreateQuery(resultExp);<br />
        }<br />
    }<br />
</code></p>
<p>This can then be called on an IQueryable object as follows (I am using a basic repository here):</p>
<p><code></p>
<p>Repository r = new Repository();<br />
            var sortedOrderDetails = r.GetEntities().OrderBy("DateShipped", GenericSorter.SortDirection.Descending);</p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pdxJaxon</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-22</link>
		<dc:creator>pdxJaxon</dc:creator>
		<pubDate>Wed, 09 Jul 2008 23:53:57 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-22</guid>
		<description>I&#039;m still having trouble with this....What is the &quot;TEntity&quot; in all these examples?

I&#039;ve been reading tons of online blogs, articles, etc and they all seem to reference this mythical thing but I can find no concrete explanation of what it is.

I get errors in code when I duplicate the code as .NET cannot resolve TEntity.</description>
		<content:encoded><![CDATA[<p>I&#8217;m still having trouble with this&#8230;.What is the &#8220;TEntity&#8221; in all these examples?</p>
<p>I&#8217;ve been reading tons of online blogs, articles, etc and they all seem to reference this mythical thing but I can find no concrete explanation of what it is.</p>
<p>I get errors in code when I duplicate the code as .NET cannot resolve TEntity.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pradeep Mishra</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-7</link>
		<dc:creator>Pradeep Mishra</dc:creator>
		<pubDate>Tue, 18 Mar 2008 00:55:27 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-7</guid>
		<description>Ernesto:
You need not define the TEntity Class. It&#039;s a source class. However you are right about syntax error. Here is the correct one

[sourcecode language=&#039;C#&#039;]
public static IQueryable&lt;TEntity&gt; OrderBy&lt;TEntity&gt;(this IQueryable&lt;TEntity&gt; source, string sortExpression) where TEntity : class
[/sourcecode]

Suppose you are applying the order by in Anonymous type 

[sourcecode language=&#039;C#&#039;]
var query = (from b in DataContext.Books select new {b.BookId, b.BookName, b.Author, b.PublishedDate});
List&lt;Book&gt; bookList = query.OrderBy(sortExpression).ToList();
[/sourcecode]
The query returns anonymous type containing values of type  So this defines TEntity Here. Let me know if it helps you. If you still have doubts let me know.
Thanks a lot for pointing out the error in code. I will update the pose with the correct code.

Danijel:
There was small correction in code pointed out by Ernesto. You can write this static method in an Utility class in the class library having your Entities (dbml file). Let me know if you still face any problem.</description>
		<content:encoded><![CDATA[<p>Ernesto:<br />
You need not define the TEntity Class. It&#8217;s a source class. However you are right about syntax error. Here is the correct one</p>
<p>public static IQueryable<tentity> OrderBy</tentity><tentity>(this IQueryable</tentity><tentity> source, string sortExpression) where TEntity : class</p>
<p>Suppose you are applying the order by in Anonymous type </p>
<p>var query = (from b in DataContext.Books select new {b.BookId, b.BookName, b.Author, b.PublishedDate});<br />
List<book> bookList = query.OrderBy(sortExpression).ToList();</p>
<p>The query returns anonymous type containing values of type  So this defines TEntity Here. Let me know if it helps you. If you still have doubts let me know.<br />
Thanks a lot for pointing out the error in code. I will update the pose with the correct code.</p>
<p>Danijel:<br />
There was small correction in code pointed out by Ernesto. You can write this static method in an Utility class in the class library having your Entities (dbml file). Let me know if you still face any problem.</book></tentity></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danijel</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-6</link>
		<dc:creator>Danijel</dc:creator>
		<pubDate>Mon, 17 Mar 2008 14:17:21 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-6</guid>
		<description>I didn&#039;t succeeded to use OrderBy extension method in mine code, because there were no &quot;using area&quot; along with class declaration in both code examples, or something else.
If you could provide it, it will help more.
Thanks</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t succeeded to use OrderBy extension method in mine code, because there were no &#8220;using area&#8221; along with class declaration in both code examples, or something else.<br />
If you could provide it, it will help more.<br />
Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ernesto</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-5</link>
		<dc:creator>Ernesto</dc:creator>
		<pubDate>Thu, 13 Mar 2008 15:08:31 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-5</guid>
		<description>&lt;cite&gt;
Hi, I’m having a problem with the signature of your extension method, well, actually the problem is that I don’t fully understand it. Coould you explain a little further?


public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class

especially the TEntity, where is that defined, I get a sintax error there.
Thanks.
&lt;cite&gt;

I get this in the where TEntity:class(I do know this is a constrain for a generic declaration):
&quot;Constraints are not allowed on non-generic declarations&quot;</description>
		<content:encoded><![CDATA[<p><cite><br />
Hi, I’m having a problem with the signature of your extension method, well, actually the problem is that I don’t fully understand it. Coould you explain a little further?</p>
<p>public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class</p>
<p>especially the TEntity, where is that defined, I get a sintax error there.<br />
Thanks.<br />
</cite><cite></p>
<p>I get this in the where TEntity:class(I do know this is a constrain for a generic declaration):<br />
&#8220;Constraints are not allowed on non-generic declarations&#8221;</cite></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ernesto</title>
		<link>http://technoesis.wordpress.com/2008/03/03/linq-to-sql-dynamic-sorting-without-using-complete-dynamic-linq-libraries/#comment-4</link>
		<dc:creator>Ernesto</dc:creator>
		<pubDate>Thu, 13 Mar 2008 15:05:51 +0000</pubDate>
		<guid isPermaLink="false">http://technoesis.wordpress.com/?p=5#comment-4</guid>
		<description>Hi, I&#039;m having a problem with the signature of your extension method, well, actually the problem is that I don&#039;t fully understand it. Coould you explain a little further?

&lt;code&gt;
 public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class
&lt;code&gt;

especially the TEntity, where is that defined, I get a sintax error there.
Thanks.</description>
		<content:encoded><![CDATA[<p>Hi, I&#8217;m having a problem with the signature of your extension method, well, actually the problem is that I don&#8217;t fully understand it. Coould you explain a little further?</p>
<p><code><br />
 public static IQueryable OrderBy(this IQueryable source, string sortExpression) where TEntity : class<br />
</code><code></p>
<p>especially the TEntity, where is that defined, I get a sintax error there.<br />
Thanks.</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
