﻿<?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; Select</title>
	<atom:link href="http://byatool.com/tag/select/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>ANDRE SMASH!!!!</description>
	<lastBuildDate>Fri, 10 Sep 2010 18:53:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>What Is Readable</title>
		<link>http://byatool.com/pontification/what-is-readable/</link>
		<comments>http://byatool.com/pontification/what-is-readable/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 14:51:57 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Pontification]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=56</guid>
		<description><![CDATA[So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods. Don&#8217;t know what I mean? Expression: var result = from knowledge in Sean select knowledge.Linq; As opposed to: var result = Sean.Select(knowledge =&#62; knowledge.Linq); Personally I would replace the lambda expression with [...]]]></description>
			<content:encoded><![CDATA[<p>So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods.  Don&#8217;t know what I mean?</p>
<p>Expression:</p>
<pre><span style="color: #0000ff;">var</span> result = <span style="color: #0000ff;">from</span> knowledge <span style="color: #0000ff;">in</span> Sean
             <span style="color: #0000ff;">select</span> knowledge.Linq;</pre>
<p>As opposed to:</p>
<pre><span style="color: #0000ff;">var</span> result = Sean.Select(knowledge =&gt; knowledge.Linq);</pre>
<p>Personally I would replace the lambda expression with a Func, but I can live with it right now. Anywho, the argument is that the first looks better than the second. I really don&#8217;t see this as a looks problem, but a useage problem. Fact is, they both have their uses and you should know how to read both. Why is that? Well here&#8217;s an example CAUSE I KNOW YOU WANT ONE!</p>
<p>One of my earlier posts had to do with <a title="FizzBuzz" href="http://byatool.com/?p=46">solving the FizzBuzz</a> thing with Linq where I gave you this bad ass solution:</p>
<pre> <span style="color: #0000ff;">var</span> result =
      listToConvert
      .<span style="color: #000000;">Where</span>(WhereBothDivisible(fizzNumber, buzzNumber))
      .<span style="color: #000000;">Select</span>(selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>))
      .Concat(
            listToConvert
            .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
            .Select(selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>)))
            .Concat(
                  listToConvert
                  .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
                  .Select(selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>)))
                  .Concat(
                         listToConvert
                        .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
                        .Select(selectKeyValuePair(<span style="color: #ff0000;">"Nothing"</span>)));</pre>
<p>As you can see, I&#8217;ve used both Func fields and methods to return Funcs to clean up how this would look. I&#8217;ll even show what it would look like without this approach:</p>
<pre><span style="color: #0000ff;">var</span> result = listToConvert.Where(currentItem =&gt;
             IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber)
             ).Select(currentItem =&gt; new KeyValuePair(currentItem, <span style="color: #ff0000;">"FizzBuzz"</span>)).Concat(...</pre>
<p>Now I can totally admit that this second one I am showing is just ouch. So the first lesson to be learn is that Funcs and Methods that return Funcs can significantly clean up the Linq Method approach.</p>
<p>Now you could do the same with expressions:</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>);

 <span style="color: #0000ff;">var</span> buzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>);

 <span style="color: #0000ff;">var</span> fizz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereFizzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);

<span style="color: #0000ff;">var</span> neither = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
              <span style="color: #0000ff;">where</span> WhereNeitherDivisable(fizzNumber, buzzNumber)
              <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);</pre>
<p>Ok so nice and pretty, but now what? Concatenation. This is where is gets ugly:</p>
<pre>  fizzBuzz.Concat(fizz.Concat(buzz.Concat(neither))));</pre>
<p>OR</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>)
                .Concat(
                     <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                     <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                     <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>))
                     .Concat(....);</pre>
<p>See what I&#8217;m getting at? The non expression one is looking a bit better now or maybe this is a fight to see which is less fugly. Now I admit that this may not be the best FizzBuzz solution, but it gives an example or where the Linq queries can go very wrong.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/uhg-it-wont-end/" title="Uhg It Won&#8217;t End">Uhg It Won&#8217;t End</a></li><li><a href="http://byatool.com/pontification/what-is-readable-addon/" title="What Is Readable Addon">What Is Readable Addon</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/pontification/what-is-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beyond the wall</title>
		<link>http://byatool.com/lessons/beyond-the-wall/</link>
		<comments>http://byatool.com/lessons/beyond-the-wall/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 13:41:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/02/beyond-the-wall/</guid>
		<description><![CDATA[So I never gave a solution to this problem and thought I might do that real fast. If you recall, this was the main sticking point of creating a Func for a select clause: Func&#60;User, EHH??&#62; selectUserID = currentUser =&#62; new { currentUser.ID, currentUser.UserName }; Well there is no one solution to this, but there [...]]]></description>
			<content:encoded><![CDATA[<p>So I never gave a solution to<a href="http://byatool.blogspot.com/2008/08/and-then-you-hit-wall.html"> this problem</a> and thought I might do that real fast.</p>
<p>If you recall, this was the main sticking point of creating a Func for a select clause:</p>
<pre><span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">EHH??</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> { currentUser.ID, currentUser.UserName };</pre>
<p>Well there is no one solution to this, but there is an easy and clean solution:</p>
<pre> <span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> UserQueryItem
 {
   <span style="color: #3333ff;">public</span> UserQueryItem ( <span style="color: #00cccc;">Int32</span> userID, <span style="color: #00cccc;">String</span> userName )
   {
      UserID = userID;
      UserName = userName;
   }

   <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">Int32</span> UserID { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
   <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">String</span> UserName { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
 }</pre>
<p>Create a class to hold the information.</p>
<pre> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">UserQueryItem</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">UserQueryItem</span> { UserID = currentUser.ID, UserName = currentUser.UserName };</pre>
<p>Or</p>
<pre>
 <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">UserQueryItem</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">UserQueryItem</span> (currentUser.ID, currentUser.UserName);</pre>
<p>Pretty simple, just a little more work.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/uhg-it-wont-end/" title="Uhg It Won&#8217;t End">Uhg It Won&#8217;t End</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/beyond-the-wall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>And then you hit the wall.</title>
		<link>http://byatool.com/lessons/and-then-you-hit-the-wall/</link>
		<comments>http://byatool.com/lessons/and-then-you-hit-the-wall/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 20:39:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Mumble Types]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/26/and-then-you-hit-the-wall/</guid>
		<description><![CDATA[So as this dynamic nonsense continues, there is a sticking point to how much fun I can have. The wall? Anonymous types and generic declarations. Here&#8217;s the old: Func&#60;User, Int32&#62; selectUserID = currentUser =&#62; currentUser.UserID; Great if I want to select userIDs, but what if I want UserIDs AND UserNames&#8230; Easy right? userList.Select(currentUser =&#62; new [...]]]></description>
			<content:encoded><![CDATA[<p>So as this dynamic nonsense continues, there is a sticking point to how much fun I can have. The wall? Anonymous types and generic declarations.</p>
<p>Here&#8217;s the old:</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;</pre>
<p>Great if I want to select userIDs, but what if I want UserIDs AND UserNames&#8230; Easy right?</p>
<pre> userList.Select(currentUser =&gt; new { currentUser.ID, currentUser.UserName });</pre>
<p>Now this is the old way, but I want the new way&#8230; IE the Func way. Problem is here</p>
<pre>  <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">EHH??</span>&gt; selectUserID = currentUser =&gt;  new { currentUser.ID, currentUser.UserName };</pre>
<p>You see, there&#8217;s a problem. What the hell do I put at the return type? Fact is, without creating a method that passes back a Func or a class that has UserName and UserID properties, I&#8217;m screwed. Now from what I read <a href="http://www.interact-sw.co.uk/iangblog/2008/03/17/lambda-inference">here </a>I think I get it. First take the func:</p>
<pre>  <span style="color: #00cccc;">Func</span>&lt;K, T&gt;</pre>
<p>I have K and T that the compiler has to figure out what they are. Well it&#8217;s safe to say in the example User is K, but what is T? Well it has to figure that out from the Lamdba expression. The lambda expression has no idea what it is because it&#8217;s an anonymous type. So why not just use var?</p>
<pre>  <span style="color: #33ccff;">Func</span>&lt;<span style="color: #33ccff;">User</span>, <span style="color: #3333ff;">var</span>&gt;</pre>
<p>Seems easy enough. I don&#8217;t have to know the type because of var right? Wellll problem is the compiler is looking at the lambda expression to figure out what var will be. Mr. Lambda expression can&#8217;t really figure out the type either. Enter the wall. Currently there is no way around this without methods or classes created. Supposedly there are things called Mumble Types on the way that will solve this problem.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li><li><a href="http://byatool.com/general-coding/when-is-a-field-not-a-field/" title="When is a field not a field?">When is a field not a field?</a></li><li><a href="http://byatool.com/general-coding/adding-to-the-select/" title="Adding to the Select">Adding to the Select</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/and-then-you-hit-the-wall/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>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/orderby-using-a-property-name/" title="Dynamic Linq: OrderBy Using a String for a Property Name">Dynamic Linq: OrderBy Using a String for a Property Name</a></li><li><a href="http://byatool.com/general-coding/adding-to-the-select/" title="Adding to the Select">Adding to the Select</a></li><li><a href="http://byatool.com/c/i-are-tupid/" title="I are tupid">I are tupid</a></li></ul>]]></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>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/when-is-a-field-not-a-field/" title="When is a field not a field?">When is a field not a field?</a></li><li><a href="http://byatool.com/c/i-are-tupid/" title="I are tupid">I are tupid</a></li><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/adding-to-the-select/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speaking of Select</title>
		<link>http://byatool.com/general-coding/speaking-of-select/</link>
		<comments>http://byatool.com/general-coding/speaking-of-select/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 17:46:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Like]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/22/speaking-of-select/</guid>
		<description><![CDATA[So like Where and other fine Extension methods, Select allows you to either give it a lambda expression like so: List&#60;String&#62; newList = userList.Select(user =&#62; user.Name); Or List&#60;Int32&#62; newList = userList.Select(user =&#62; user.ID); So either you get a list of Names or IDs. What the hell do you care? Well if you are capable of [...]]]></description>
			<content:encoded><![CDATA[<p>So like Where and other fine Extension methods, Select allows you to either give it a lambda expression like so:</p>
<pre>  <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">String</span>&gt; newList = userList.Select(user =&gt; user.Name);</pre>
<p>Or</p>
<pre><span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; newList = userList.Select(user =&gt; user.ID);</pre>
<p>So either you get a list of Names or IDs. What the hell do you care? Well if you are capable of breathing, you should also notice that one list is a list of strings the other a list of integers. What if you wanted a generic select method? Well for starters you would do something like this:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">String</span>&gt; SelectFromUserList(<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">String</span>&gt; selectMethod, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; userList)
{
<span style="color: #3333ff;">  return</span> userList.Select(selectMethod).ToList();
}</pre>
<p>Where the select method for a user name would be something like this:</p>
<pre><span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">String</span>&gt; nameList = SelectFromUserList(currentUser =&gt; currentUser.UserName, userList);</pre>
<p>Sweet&#8230; oh wait, that only works if I want a list strings. Great if I wanted LastName, FirstName, ect but sucks if I wanted an integer ID.</p>
<pre><span style="color: #00cccc;">  List</span>&lt;<span style="color: #00cccc;">String</span>&gt; nameList = SelectFromUserList(currentUser =&gt; currentUser.UserID, userList); <span style="color: #009900;">//BOOM</span></pre>
<p>But wait, you can do that!</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)
{
<span style="color: #3333ff;">  return</span> userList.Select(selectMethod).ToList();
}</pre>
<p>Aw snap, now I can get a list of anything I want, well at least a one type, one dimensional array. By the way, this is the first step in probably a series of posts that will end up with a much cleaner way of doing this.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li><li><a href="http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/" title="Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH">Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</a></li><li><a href="http://byatool.com/general-coding/the-switch-remover/" title="The Switch Remover: Convert Switch Statements to Dictionaries">The Switch Remover: Convert Switch Statements to Dictionaries</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/speaking-of-select/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq query versus just a Select</title>
		<link>http://byatool.com/general-coding/linq-query-versus-just-a-select/</link>
		<comments>http://byatool.com/general-coding/linq-query-versus-just-a-select/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 14:07:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/22/linq-query-versus-just-a-select/</guid>
		<description><![CDATA[Something I ran into using the Ajax.dll and AjaxMethods (Or basically the idea of being able to call a method from .cs file in javascript) is that when returning a list of Users there was difficulty in serialization. Basically, if you have a list of users that contain a list of roles that contain a [...]]]></description>
			<content:encoded><![CDATA[<p>Something I ran into using the Ajax.dll and AjaxMethods (Or basically the idea of being able to call a method from .cs file in javascript) is that when returning a list of Users there was difficulty in serialization. Basically, if you have a list of users that contain a list of roles that contain a list of permissions that contain seven wives with seven children with seven dogs and seven cats&#8230; Basically a lot of information. Now lazy loading should take care of this, but lets assume there is no lazy loading.(No idea what would cause that&#8230;) Well you have an awful lot to pushout to the front end. Let&#8217;s just say it can be a little sluggish. Lets assume you are just sending the list out, and you&#8217;ve already done all the querying.</p>
<p>You could do:</p>
<pre> <span style="color: #3333ff;">return</span> userList;</pre>
<p>Which is what I was doing. Kind of slow.</p>
<p>You could do:</p>
<pre> <span style="color: #3333ff;">var</span> query = <span style="color: #3333ff;">from</span> user <span style="color: #3333ff;">in</span> userList
             <span style="color: #3333ff;">select</span> <span style="color: #3333ff;">new</span>
             {
                UserName = user.UserName,
                UserID = user.ID
             };

 <span style="color: #3333ff;">return</span> query.ToList();</pre>
<p>Nothing wrong with that, just a bit verbose for something so simple.</p>
<p>You could do this:</p>
<pre> <span style="color: #3333ff;">return</span> userList.Select(currentItem =&gt; <span style="color: #3333ff;">new</span> { Username = currentItem.UserName, UserID = user.ID });</pre>
<p>All in one line. Now I personally like the sql-ish linq expressions, but for this why not fit it into one line?</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li><li><a href="http://byatool.com/lessons/and-then-you-hit-the-wall/" title="And then you hit the wall.">And then you hit the wall.</a></li><li><a href="http://byatool.com/general-coding/when-is-a-field-not-a-field/" title="When is a field not a field?">When is a field not a field?</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/linq-query-versus-just-a-select/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sub Select and IN Clause With Linq</title>
		<link>http://byatool.com/general-coding/sub-select-and-in-clause-faking/</link>
		<comments>http://byatool.com/general-coding/sub-select-and-in-clause-faking/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 16:59:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Clause]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/09/sub-select-and-in-clause-faking/</guid>
		<description><![CDATA[I have yet to find a good way to represent an IN clause in Linq, but I found this yesterday and kind of liked it. Mind you, I&#8217;ve used this only on two lists, not database involved. Will check what it does on the database call later. Anyhow, I needed a way to check if [...]]]></description>
			<content:encoded><![CDATA[<p>I have yet to find a good way to represent an IN clause in Linq, but I found this yesterday and kind of liked it. Mind you, I&#8217;ve used this only on two lists, not database involved. Will check what it does on the database call later.</p>
<p>Anyhow, I needed a way to check if the records in one list are the same as the other. I&#8217;m sure there are a billion ways to do this, but I wanted a Linq way. I stumbled onto this idea when looking for a solution to something else. Basically I have two lists of users and a user contains a UserID. listOne and listTwo are both <span style="color: #3333ff;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt;.</p>
<pre><span style="color: #3333ff;">var</span> query = <span style="color: #3333ff;">from</span> listOneUser <span style="color: #3333ff;">in</span> listOne
            <span style="color: #3333ff;">where</span>
            !(
                <span style="color: #3333ff;">from</span> listTwoUser <span style="color: #3333ff;">in</span> listTwo
                <span style="color: #3333ff;">select</span> listTwoUser.UserID
            ).Contains(listOneUser.UserID)
              <span style="color: #3333ff;">select</span> listOneUser;</pre>
<p>I select all the IDs from the second list and then see if the first list has any users that don&#8217;t exist in the second list. If this query gives me a list with a count greater then 0, I know that list one has at least one different item. Again, this isn&#8217;t bullet proof, just a way to show the kind of IN clause.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li><li><a href="http://byatool.com/lessons/and-then-you-hit-the-wall/" title="And then you hit the wall.">And then you hit the wall.</a></li><li><a href="http://byatool.com/general-coding/when-is-a-field-not-a-field/" title="When is a field not a field?">When is a field not a field?</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/sub-select-and-in-clause-faking/feed/</wfw:commentRss>
		<slash:comments>1</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>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/when-is-a-field-not-a-field/" title="When is a field not a field?">When is a field not a field?</a></li><li><a href="http://byatool.com/general-coding/adding-to-the-select/" title="Adding to the Select">Adding to the Select</a></li><li><a href="http://byatool.com/pontification/what-is-readable/" title="What Is Readable">What Is Readable</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/c/i-are-tupid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
