﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Order By</title>
	<atom:link href="http://byatool.com/tag/order-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Dynamic Linq: OrderBy Using a String for a Property Name</title>
		<link>http://byatool.com/general-coding/orderby-using-a-property-name/</link>
		<comments>http://byatool.com/general-coding/orderby-using-a-property-name/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 13:39:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Order By]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/10/01/orderby-using-a-property-name/</guid>
		<description><![CDATA[Now this is kind of dangerous to do since there is no compile time check (Like most things set in markup) but say you want to sort a collection, using the Linq extension methods, but you don&#8217;t know what you what to sort on at any given time. On top of that, you have a [...]]]></description>
			<content:encoded><![CDATA[<p>Now this is kind of dangerous to do since there is no compile time check (Like most things set in markup) but say you want to sort a collection, using the Linq extension methods, but you don&#8217;t know what you what to sort on at any given time. On top of that, you have a datagrid and a bunch of sort expressions to deal with. Now you could do something like create a hashtable full of lambda expressions that the key is the sort expression:</p>
<pre><span style="color: #00cccc;">Dictionary</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">IComparable</span>&gt;&gt; list;

userList = <span style="color: #00cccc;">User</span>.GetUserList();
list = <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">Dictionary</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">IComparable</span>&gt;&gt;();
list.Add(<span style="color: #990000;">"UserName"</span>, currentUser =&gt; currentUser.UserName);
list.Add(<span style="color: #990000;">"UserID"</span>, currentUser =&gt; currentUser.UserID);
userList.OrderBy(list[<span style="color: #990000;">"UserID"</span>]);</pre>
<p>Works just fine, and might be preferable to what I&#8217;m about to show. OooOoOO sound eerie?</p>
<pre><span style="color: #009900;">//This is just to get the property info using reflection.  In order to get the value</span>
<span style="color: #009900;">//from a property dynamically, we need the property info from the class</span>
<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">PropertyInfo</span>[] GetInfo&lt;K&gt;(K item) <span style="color: #3333ff;">where</span> K : <span style="color: #3333ff;">class</span>
{
  <span style="color: #00cccc;">PropertyInfo</span>[] propertyList;
  <span style="color: #00cccc;">Type</span> typeInfo;

  typeInfo = item.GetType();
  propertyList = typeInfo.GetProperties();

  <span style="color: #3333ff;">return</span> propertyList;
}

<span style="color: #009900;">//This is the dynamic order by func that the OrderBy method needs to work</span>
<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">IComparable</span> OrderByProperty&lt;T&gt;(<span style="color: #00cccc;">String</span> propertyName, T item)
  <span style="color: #3333ff;">where</span> T : <span style="color: #3333ff;">class</span>
{
  <span style="color: #00cccc;">PropertyInfo</span>[] propertyList;

  propertyList = GetInfo(item);

  <span style="color: #009900;">//Here we get the value of that property of the passed in item and make sure</span>
  <span style="color: #009900;">//to type the object (Which is what GetValue returns) into an IComparable</span>
  return (<span style="color: #00cccc;">IComparable</span>)propertyList.First(currentProperty
    =&gt; currentProperty.Name == propertyName).GetValue(item, <span style="color: #3333ff;">null</span>);
}</pre>
<p>And use:</p>
<pre><span style="color: #009900;">//This takes the current user and calls the OrderByProperty method which in turn</span>
<span style="color: #009900;">//gives us the Func OrderBy is requesting.</span>
<span style="color: #3333ff;">var</span> test = userList.OrderBy(currentUser
  =&gt; DynamicPropertySort.OrderByProperty(<span style="color: #990000;">"UserID"</span>, currentUser)).ToList();</pre>
<p>Ok so what the hell? I mean intellisense on the OrderBy method doesn&#8217;t give much help. <span style="color: #00cccc;">Func</span>&lt;&lt;<span style="color: #00cccc;">User</span>, TKey&gt;&gt;. Yeah ok. So basically the return type is open. Well this kind of sucks right? Because I would have to return a Func that already knows the return type. (Be it string, int, ect) Of course, this would mean we would have to handle each sort expression in code. NOT VERY DYNAMIC IS IT? Well f that. Truth is, what the order by is looking for is a Func that takes in a User and returns something it can compare. This is where <span style="color: #00cccc;">IComparable </span>comes in.</p>
<p>The OrderBy has to take the returned value, say UserID which is an int, and figure out how to compare it to another value. Pretty simple. So as long as the property you are ordering by uses <span style="color: #00cccc;">IComparable</span>, you&#8217;re good to go. Pretty nice huh?</p>
<p>Now I would suggest, if you use this (HAHAHAHA), is to cache a dictionary of the property info with the class type as the key so that you don&#8217;t have to use as much reflection everytime. I just didn&#8217;t put that in.</p>
<p>U U USING</p>
<pre><span style="color: #3333ff;">using</span> System;
<span style="color: #3333ff;">using</span> System.Collections.Generic;
<span style="color: #3333ff;">using</span> System.Linq;
<span style="color: #3333ff;">using</span> System.Reflection;
<span style="color: #3333ff;">using</span> System.Text;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/orderby-using-a-property-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When is a field not a field?</title>
		<link>http://byatool.com/general-coding/when-is-a-field-not-a-field/</link>
		<comments>http://byatool.com/general-coding/when-is-a-field-not-a-field/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 12:18:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Order By]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/26/when-is-a-field-not-a-field/</guid>
		<description><![CDATA[Ok so for the last few things I&#8217;ve been showing a more dynamic approach to linq queries , mostly dealing with collections rather than say linq to sql. Now take the new method: public List&#60;K&#62; SelectFromUserList&#60;K, L&#62;(Func&#60;User, K&#62; selectMethod, Func&#60;User, L&#62; orderBy, List&#60;User&#62; userList) { List&#60;K&#62; userList = new List&#60;K&#62;(); userList = userList.OrderBy(orderBy).Select(selectMethod).ToList(); return userList; [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so for the last few things I&#8217;ve been showing a more dynamic approach to linq queries , mostly dealing with collections rather than say linq to sql. Now take the new method:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #00cccc;">List</span>&lt;K&gt; SelectFromUserList&lt;K, L&gt;(<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, K&gt; selectMethod, <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, L&gt; orderBy, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; userList)
{
<span style="color: #00cccc;">    List</span>&lt;K&gt; userList = <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">List</span>&lt;K&gt;();
    userList = userList.OrderBy(orderBy).Select(selectMethod).ToList();

<span style="color: #3333ff;">    return</span> userList;
}</pre>
<p>and the call was this:</p>
<pre><span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">String</span>&gt; newList = userList.Select(selectUser =&gt; selectUser.Name, orderUser =&gt; orderUser.ID, userList);</pre>
<p>Let&#8217;s say you have two needs, selecting all the userNames and all the IDs. You could go ahead and call that method twice and inserting the lambda expressions. But let&#8217;s say you want to be able to mix and match things. Say select user names and order by user id or maybe select user names and order by use names. Well there&#8217;s a solution to avoid rewriting the lambda expressions:</p>
<pre><span style="color: #00cccc;">  Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">Int32</span>&gt; selectUserID = currentUser =&gt; currentUser.UserID;
<span style="color: #00cccc;">  Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">String</span>&gt;selectUserName = currentUser =&gt; currentUser.UserName;

<span style="color: #00cccc;">  Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">Int32</span>&gt; orderByUserID = currentUser =&gt; currentUser.UserID;
<span style="color: #00cccc;">  Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">String</span>&gt; orderByUserName = currentUser =&gt; currentUser.UserName;</pre>
<p>What the? See a while ago I had a method to pass back the expression, but in this case there&#8217;s nothing to create the expression from (a passed in parameter) since they are really simple expressions. How would I use them?</p>
<pre>
<span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; userIDs;
<span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">User</span>&gt; userList;
<span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">String</span>&gt; userNames;
  userIDs = SelectFromUserList(selectUserID, orderByUserID, userList);
  userNames = SelectFromUserList(selectUserName, orderByUserID, userList);</pre>
<p>Pretty nice huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/when-is-a-field-not-a-field/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding to the Select</title>
		<link>http://byatool.com/general-coding/adding-to-the-select/</link>
		<comments>http://byatool.com/general-coding/adding-to-the-select/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 12:37:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Order By]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/25/adding-to-the-select/</guid>
		<description><![CDATA[So in the last post there was something like this: public List&#60;K&#62; SelectUserNameList&#60;K&#62;(Func&#60;User, K&#62; selectMethod, List&#60;User&#62; userList) { return userList.Select(selectMethod, userList).ToList(); } Called by this: List&#60;String&#62; newList = userList.Select(user =&#62; user.Name, userList); Ok so what if you want to order something? Well same idea, just another Func. But remember, a Func that can order by [...]]]></description>
			<content:encoded><![CDATA[<p>So in the last post there was something like this:</p>
<pre> <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">List</span>&lt;K&gt; SelectUserNameList&lt;K&gt;(<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, K&gt; selectMethod, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; userList)
 {
   return userList.Select(selectMethod, userList).ToList();
 }</pre>
<p>Called by this:</p>
<pre> <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">String</span>&gt; newList = userList.Select(user =&gt; user.Name, userList);</pre>
<p>Ok so what if you want to order something? Well same idea, just another Func. But remember, a Func that can order by any type. If you look at the OrderBy method, it expects a <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, L&gt; where L is just some type you are returning. If you were ordering by UserID, well that would be an integer. Problem is, like select, you don&#8217;t want to be held down by a specific type.</p>
<pre> <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">List</span>&lt;K&gt; SelectFromUserList&lt;K, L&gt;(<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, K&gt; selectMethod, <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, L&gt; orderBy, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; userList)
 {
   <span style="color: #00cccc;">List</span>&lt;K&gt; userList = <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">List</span>&lt;K&gt;();

   userList = userList.OrderBy(orderBy).Select(selectMethod).ToList();

   <span style="color: #3333ff;">return</span> userList;
 }</pre>
<p>And the new call would be:</p>
<pre> <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">String</span>&gt; newList = userList.Select(selectUser =&gt; selectUser.Name, orderUser =&gt; orderUser.ID, userList);</pre>
<p>Now something of note is the order in which you call the methods. Remember, x.OrderBy().Select() is saying take collection X, order it by L and create a new collection of X, then select whatever you want. You can not do the reverse. Say you want to select a list of UserNames ordered by UserName. Well you can either:</p>
<p>1) Order the list by user name in a list of users then select the user names.</p>
<p>2) Select a list of user names into a list of strings, order that list by a string.</p>
<p>What if you want to select user names but order by user id? You can:</p>
<p>1) Order the list by user id and create a list of users then select the user names.</p>
<p>2) Select a list of user names into a list of string and&#8230; eh LOSE</p>
<p>Best part about:</p>
<pre> userList = userList.Select(selectMethod).OrderBy(orderBy).ToList();</pre>
<p>Is that the error is somewhat misleading. It gives you the &#8220;Cannot be inferred by usage&#8221; error, not the &#8220;Idiot, you can&#8217;t order a list of Strings by UserID&#8221;. So you have to be careful on how you have things in order.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/adding-to-the-select/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I are tupid</title>
		<link>http://byatool.com/c/i-are-tupid/</link>
		<comments>http://byatool.com/c/i-are-tupid/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 18:58:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Order By]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[Stupid]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/07/i-are-tupid/</guid>
		<description><![CDATA[This may be novel or really dumb, but I like it. Say you want to convert a Dictionary to a List of KeyValuePairs that are sorted by something within the dictionary Key or Value. Don&#8217;t ask why, just go with it. You could do this: Where someDictionary is Dictionary&#60;Type, string&#62; : List&#60;KeyValuePair&#60;Type, String&#62;&#62; dataSource = [...]]]></description>
			<content:encoded><![CDATA[<p>This may be novel or really dumb, but I like it. Say you want to convert a Dictionary to a List of KeyValuePairs that are sorted by something within the dictionary Key or Value. Don&#8217;t ask why, just go with it. You could do this:</p>
<p>Where someDictionary is Dictionary&lt;Type, string&gt; :</p>
<pre>
<span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #00cccc;">String</span>&gt;&gt; dataSource = new <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #00cccc;">String</span>&gt;&gt;(someDictionary);
dataSource.Sort(<span style="color: #3333ff;">new </span><span style="color: #00cccc;">SomeComparer</span>&lt;<span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #00cccc;">String</span>&gt;&gt;(<span style="color: #ff0000;">"Value"</span>, <span style="color: #3333ff;">true</span>));</pre>
<p>To:</p>
<pre><span style="color: #3366ff;">var</span> query = <span style="color: #3333ff;">from </span>keyValuePair <span style="color: #3333ff;">in</span> someDictionary
          <span style="color: #3333ff;">orderby </span>keyValuePair.Value
          <span style="color: #3333ff;">select new </span><span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #3333ff;">String</span>&gt;(keyValuePair.Key, keyValuePair.Value);
SomeMethod(query.ToList());</pre>
<p>If nothing else, you don&#8217;t have to create or implement a System.Collections.IComparer class to use the .Sort method. That seems worth it. That and I think it just plain looks better, but that just me. If I am completely wrong here, refer to the title of this post. Just a thought really.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/c/i-are-tupid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

