<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6294677086277828431</id><updated>2012-01-25T09:05:04.139-08:00</updated><category term='FizzBuzz'/><category term='Random'/><category term='func'/><category term='Contains'/><category term='Join'/><category term='Enumeration'/><category term='Covariance'/><category term='Javascript'/><category term='dynamic'/><category term='Contravariance'/><category term='hash'/><category term='String'/><category term='Session'/><category term='Generics'/><category term='Clause'/><category term='Select'/><category term='Anonymous Types'/><category term='Reading Is Fundemental'/><category term='c#'/><category term='test'/><category term='Reflection'/><category term='Order By'/><category term='Exception'/><category term='TryParse'/><category term='Linq'/><category term='Expression'/><category term='action'/><category term='Lambda'/><category term='3.0'/><category term='Union'/><category term='.net'/><category term='Like'/><category term='Windows Service'/><category term='project'/><category term='Property Initializers'/><category term='Mumble Types'/><category term='closures'/><category term='password'/><category term='Anonymous Methods'/><category term='Dynamic Controls'/><category term='stupid'/><title type='text'>Programming By A Tool</title><subtitle type='html'>This is the record of my attempts at .net 3.5 and beyond.  Not for super advanced programmers unless you're here to learn me good.  Everyone needs some come uppins sometimes.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>43</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-128901505749985542</id><published>2008-10-11T08:34:00.001-07:00</published><updated>2008-10-11T08:35:42.249-07:00</updated><title type='text'>I've moved</title><content type='html'>For those 3 people that have actually found this place, I moved to a new place.

&lt;a href="http://www.byatool.com"&gt;http://www.byatool.com&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-128901505749985542?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/128901505749985542/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=128901505749985542' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/128901505749985542'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/128901505749985542'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/10/ive-moved.html' title='I&apos;ve moved'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-4710979638054950602</id><published>2008-10-03T10:23:00.000-07:00</published><updated>2008-10-03T11:00:34.325-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reading Is Fundemental'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>Fabulous Adventures in Reading</title><content type='html'>So on my new favoritest site ever for the week &lt;a href="http://www.stackoverflow.com/"&gt;StackOverflow&lt;/a&gt; someone asked if this could be done in C#:
&lt;pre&gt;
$patterns[0] = '/=C0/';
$patterns[1] = '/=E9/';
$patterns[2] = '/=C9/';


$replacements[0] = 'à';
$replacements[1] = 'é';
$replacements[2] = 'é';
return preg_replace($patterns, $replacements, $text);
&lt;/pre&gt;
I thought this could be an interesting challenge with Linq.  And all things can be solved with Linq.  Also, this post provides a valuable moral at the end.
&lt;br /&gt;&lt;br /&gt;
So what the hell is that?  Basically the idea is to take in some text, you know the stuff you send to people on your phone, and replace one set of characters with another.  Make sense talk:  TAKE TEXT! SMASH BAD THINGS! PUT GOOD THINGS IN!
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; text;
&lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;&amp;gt; patternsToReplace;
&lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;&amp;gt; patternsToUse;

patternsToReplace = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;&amp;gt;();
patternsToReplace.Add(&lt;span style="color: rgb(204, 0, 0);"&gt;'a'&lt;/span&gt;);
patternsToReplace.Add(&lt;span style="color: rgb(204, 0, 0);"&gt;'c'&lt;/span&gt;);
patternsToUse = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;&amp;gt;();
patternsToUse.Add(&lt;span style="color: rgb(204, 0, 0);"&gt;'X'&lt;/span&gt;);
patternsToUse.Add(&lt;span style="color: rgb(204, 0, 0);"&gt;'Z'&lt;/span&gt;);

text = &lt;span style="color: rgb(204, 0, 0);"&gt;"This is a thing to replace stuff with"&lt;/span&gt;;

&lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; allAsAndCs = text.ToCharArray()
               .Select
               (
                 currentItem =&gt; patternsToReplace.Contains(currentItem)
                   ? patternsToUse[patternsToReplace.IndexOf(currentItem)]
                   : currentItem
               )
               .ToArray();
        
text = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;(allAsAndCs);
&lt;/pre&gt;
It's actually very simple.  First convert the text into a character array.  Then select through them one by one.  If the list of ones to be replaced (patternsToReplace) just happens to have the current character, replace it with the corresponding one from the replacements (patternsToUse) otherwise just return the original.
&lt;br /&gt;&lt;br /&gt;
Now the annoying thing about this example is that in preg_replace the two lists have no real correlation.  It just assumes you want and index to index match.  Therefore patternsToUse[0] replaces patternsToReplace[0].  But hey, I didn't invent the thing.
&lt;br /&gt;&lt;br /&gt;
Another note is that instead of using &lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;[] I used &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Char&lt;/span&gt;&amp;gt; because arrays don't have index of and it saves the one step of charArray.ToList().IndexOf. I'm lazy and I like lists.
&lt;br /&gt;&lt;br /&gt;
Now for the moral of the story:
&lt;br /&gt;&lt;br /&gt;
'Course I dove right into Linq and responded...  not reading that he/she needed a 2.0 solution and ultimately gave the client NOTHING THAT HE/SHE WANTED.  So, if you take anything from this... and you probably won't... Always read the requirements first.
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;br /&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;br /&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-4710979638054950602?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/4710979638054950602/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=4710979638054950602' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4710979638054950602'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4710979638054950602'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/10/so-on-my-new-favoritest-site-ever-for.html' title='Fabulous Adventures in Reading'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-6735117181325062658</id><published>2008-10-01T06:39:00.000-07:00</published><updated>2008-10-01T07:27:45.480-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='Order By'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>OrderBy using a Property Name</title><content type='html'>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'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:

&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;IComparable&lt;/span&gt;&amp;gt;&amp;gt; list;

userList = &lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;.GetUserList();
list = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;IComparable&lt;/span&gt;&amp;gt;&amp;gt;();
list.Add(&lt;span style="color: rgb(153, 0, 0);"&gt;"UserName"&lt;/span&gt;, currentUser =&gt; currentUser.UserName);
list.Add(&lt;span style="color: rgb(153, 0, 0);"&gt;"UserID"&lt;/span&gt;, currentUser =&gt; currentUser.UserID);
userList.OrderBy(list[&lt;span style="color: rgb(153, 0, 0);"&gt;"UserID"&lt;/span&gt;]);
&lt;/pre&gt;
Works just fine, and might be preferable to what I'm about to show. OooOoOO sound eerie?
&lt;pre&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//This is just to get the property info using reflection.  In order to get the value&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//from a property dynamically, we need the property info from the class&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;PropertyInfo&lt;/span&gt;[] GetInfo&amp;lt;K&amp;gt;(K item) &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; K : &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt;
{
  &lt;span style="color: rgb(0, 204, 204);"&gt;PropertyInfo&lt;/span&gt;[] propertyList;
  &lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt; typeInfo;
        
  typeInfo = item.GetType();
  propertyList = typeInfo.GetProperties();
        
  &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; propertyList;
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//This is the dynamic order by func that the OrderBy method needs to work&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IComparable&lt;/span&gt; OrderByProperty&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; propertyName, T item)
  &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; T : &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt;
{
  &lt;span style="color: rgb(0, 204, 204);"&gt;PropertyInfo&lt;/span&gt;[] propertyList;
        
  propertyList = GetInfo(item);

  &lt;span style="color: rgb(0, 153, 0);"&gt;//Here we get the value of that property of the passed in item and make sure&lt;/span&gt;
  &lt;span style="color: rgb(0, 153, 0);"&gt;//to type the object (Which is what GetValue returns) into an IComparable&lt;/span&gt;
  return (&lt;span style="color: rgb(0, 204, 204);"&gt;IComparable&lt;/span&gt;)propertyList.First(currentProperty
    =&gt; currentProperty.Name == propertyName).GetValue(item, &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;);
}
&lt;/pre&gt;
And use:
&lt;pre&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//This takes the current user and calls the OrderByProperty method which in turn&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//gives us the Func OrderBy is requesting.&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; test = userList.OrderBy(currentUser
  =&gt; DynamicPropertySort.OrderByProperty(&lt;span style="color: rgb(153, 0, 0);"&gt;"UserID"&lt;/span&gt;, currentUser)).ToList();
&lt;/pre&gt;
Ok so what the hell?  I mean intellisense on the OrderBy method doesn't give much help.  &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, TKey&amp;gt;&amp;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 &lt;span style="color: rgb(0, 204, 204);"&gt;IComparable &lt;/span&gt;comes in.

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 &lt;span style="color: rgb(0, 204, 204);"&gt;IComparable&lt;/span&gt;, you're good to go.  Pretty nice huh?

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't have to use as much reflection everytime.   I just didn't put that in.

U U USING
&lt;pre&gt;

&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Reflection;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Text;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-6735117181325062658?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/6735117181325062658/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=6735117181325062658' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6735117181325062658'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6735117181325062658'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/10/orderby-using-property-name.html' title='OrderBy using a Property Name'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-5477280463942789330</id><published>2008-09-30T06:46:00.000-07:00</published><updated>2008-09-30T07:03:27.703-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><title type='text'>More Fun With Linq</title><content type='html'>Say you have a class named BannedProgram and it has a collection of DayOfWeek and a string ProcessName.  Now the collection of DayOfWeek is basically a way to set the days of the week it's banned.  With this you want to create a collection of these BannedPrograms, each with their own names and days they are banned.  Simple, I know.

Next you have a list of processes that are currently running and you want to get all the processes that match the names in the BannedPrograms list AND if the current day is a banned day.

First you need the day checked function:
&lt;pre&gt;

&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;DayOfWeek&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; dayIsToday = 
  currentDay =&gt; currentDay == DateTime.Now.DayOfWeek;

&lt;/pre&gt;

Then you need the method to get the banned processes that are currently running:

&lt;pre&gt;

&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Process&lt;/span&gt;[] GetBannedProcesses(&lt;span style="color: rgb(0, 204, 204);"&gt;BannedProgram&lt;/span&gt;[] programs, &lt;span style="color: rgb(0, 204, 204);"&gt;Process&lt;/span&gt;[] processes)
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; processList = &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; process &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; processes
  &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt;
  (
    &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; program &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; programs
    &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; program.DaysBanned.Any(dayIsToday)
    &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; program.ProcessName
  ).Contains(process.ProcessName)
  &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; process;

  &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; processList.ToArray();
}
&lt;/pre&gt;
What this is doing:

Well if you look at this:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; program &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; programs
&lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; program.DaysBanned.Any(dayIsToday)
&lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; program.ProcessName
&lt;/pre&gt;
This is going to grab any BannedProgram that has a DayOfWeek that matches today and it will select only it's name.  This will give you a list of names of the BannedProcesses that can not be played today.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; processList = &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; process &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; processes
&lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt;
(
  &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; program &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; programs
  &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; program.DaysBanned.Any(dayIsToday)
  &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; program.ProcessName
).Contains(process.ProcessName)
&lt;/pre&gt;
This checks to see if any of the currently running processes have a name that matches a name in the banned program list.

And now you have a list of processes to kill.  Yay.  Not sure this is a big deal, just thought it was a fun example of using linq and subselects.

USING???
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.ServiceProcess;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Windows.Forms;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-5477280463942789330?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/5477280463942789330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=5477280463942789330' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5477280463942789330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5477280463942789330'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/09/more-fun-with-linq.html' title='More Fun With Linq'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-5053074970099923850</id><published>2008-09-30T06:21:00.000-07:00</published><updated>2008-09-30T06:35:33.103-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windows Service'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>More Useless info</title><content type='html'>Just in case you wanted to create something to kill a process, I have this little bit:

&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Diagnostics;

&lt;span style="color: rgb(0, 204, 204);"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Process&lt;/span&gt;&amp;gt; killProcess = currentProcess =&gt; currentProcess.Kill();
&lt;span style="color: rgb(0, 204, 204);"&gt;Process&lt;/span&gt;[] processes;
&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; processToKill;

processToKill = &lt;span style="color: rgb(153, 0, 0);"&gt;"WAR"&lt;/span&gt;;
processes = Process.GetProcesses();

processes.Select(currentProcess =&gt; currentProcess.ProcessName == processToKill)
.ToList()
.ForEach(killProcess);

&lt;/pre&gt;

I originally intended this as a service that would kill processes that were running on a specific day.  IE To stop myself from playing games during the week.  Problem was finding a way to stop me from killing the service.  Now you can set a service to disallowing stopping it.  The idea being that I would have another program that would stop it only if a password was enter correctly. (A certain woman would have this password)  Trouble is, I can just open up the task manager and kill the process.  Now I have to rely on self control.  WHICH IS THE REASON WHY I WANTED THIS IN THE FIRST PLACE.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-5053074970099923850?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/5053074970099923850/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=5053074970099923850' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5053074970099923850'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5053074970099923850'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/09/more-useless-info.html' title='More Useless info'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1946494756580480266</id><published>2008-09-09T13:17:00.000-07:00</published><updated>2008-09-10T06:56:31.153-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Expression'/><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><title type='text'>Combining Lambda Expressions</title><content type='html'>Found this post &lt;a href="http://www.albahari.com/nutshell/predicatebuilder.html"&gt;here&lt;/a&gt; but wanted to make a really simple example to demonstrate this.

The idea is simple, take something like this:
&lt;pre&gt;
currentItem =&gt; currentItem.BooleanMethodOne() &amp;amp;&amp;amp; currentItem.BooleanMethodTwo()
&lt;/pre&gt;
but say you only want to have one clause or both.  Well you could make three separate expressions, but what if you wanted to add even more later?  What if you wanted to mix and match?  What if you're reading thing because you watched to see what page could possibly be on the last page of a google search?  Well I have answers... stolen answers.

First the needed And and Or methods:
&lt;pre&gt;

&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; And&amp;lt;T&amp;gt;(
&lt;span style="color: rgb(0, 204, 204);"&gt;   Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; expressionOne,
&lt;span style="color: rgb(0, 204, 204);"&gt;    Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; expressionTwo
)
{
&lt;span style="color: rgb(0, 153, 0);"&gt;  //Basically this is like a bridge between the two expressions.  It will take the T&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //parameter from expressionOne and apply it to expression two. So if &lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  // oneItem =&gt; oneItem.OneMethod() is expressionOne&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  // twoItem =&gt; twoItem.TwoMethod() is expressionTwo&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //it would be like replacing the twoItem with the oneItem so that they now point&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //to the same thing.&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;  var&lt;/span&gt; invokedSecond = &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;.Invoke(expressionTwo, expressionOne.Parameters.Cast&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;gt;());
  
&lt;span style="color: rgb(0, 153, 0);"&gt;  //Now this is to create the needed expresions to return.  It will take both early expressions&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //and use the item from the first expression in both.&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;                    &lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //It will look something like this:&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //currentItem =&gt; (currentItem.OneMethod And Invoke(currentItem =&gt; currentItem.TwoMethod()))&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //As you can see, it looks to be running expressionOne and then a new method that basically&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //calls expressionTwo with the same value (currentItem)&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;  return&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;
   Expression&lt;/span&gt;.And(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}

&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expressio&lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;n&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; Or&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;
Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; expressionOne, &lt;span style="color: rgb(0, 204, 204);"&gt;
Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Fun&lt;/span&gt;c&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; expressionTwo
)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  var&lt;/span&gt; invokedSecond = &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;.Invoke(expressionTwo, expressionOne.Parameters.Cast&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;gt;());

&lt;span style="color: rgb(51, 51, 255);"&gt;  return&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt;(
&lt;span style="color: rgb(0, 204, 204);"&gt;   Expression&lt;/span&gt;.Or(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}
&lt;/pre&gt;
And here's a test for it:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;String&lt;/span&gt;[] list;
  
list = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;String&lt;/span&gt;[] { &lt;span style="color: rgb(153, 0, 0);"&gt;"a"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"b"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"c"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"ac"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"ab"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"cc"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"d"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"dd"&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;"dc"&lt;/span&gt; };

&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; stringLikeA = currentString =&gt; currentString.Contains(&lt;span style="color: rgb(153, 0, 0);"&gt;"a"&lt;/span&gt;);
&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; stringLikeB = currentString =&gt; currentString.Contains(&lt;span style="color: rgb(153, 0, 0);"&gt;"b"&lt;/span&gt;);
&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; stringLikeC = currentString =&gt; currentString.Contains(&lt;span style="color: rgb(153, 0, 0);"&gt;"c"&lt;/span&gt;);

&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, Boolean&amp;gt;&amp;gt; neededUser = And&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(stringLikeA, stringLikeB);
list.Where(neededUser.Compile());

&lt;span style="color: rgb(0, 153, 0);"&gt;//a&lt;/span&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(list.Where(neededUser.Compile()).Count() == 1);  //ab

&lt;span style="color: rgb(0, 153, 0);"&gt;//a, c, ac, ab, cc, dc&lt;/span&gt;
neededUser = Or&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(stringLikeA, stringLikeC);

&lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(list.Where(neededUser.Compile()).Count() == 6);

&lt;span style="color: rgb(0, 153, 0);"&gt;//ab, c, ac, cc, dc&lt;/span&gt;
neededUser = And&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(stringLikeA, stringLikeB);
neededUser = Or&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(neededUser, stringLikeC);
&lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(list.Where(neededUser.Compile()).Count() == 5);
&lt;/pre&gt;
USINGS!!ONEONE
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq.Expressions;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; Microsoft.VisualStudio.TestTools.UnitTesting;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1946494756580480266?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1946494756580480266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1946494756580480266' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1946494756580480266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1946494756580480266'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/09/combining-lambda-expressions.html' title='Combining Lambda Expressions'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-6719865363274655884</id><published>2008-09-08T06:40:00.000-07:00</published><updated>2008-09-08T07:10:14.306-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='action'/><category scheme='http://www.blogger.com/atom/ns#' term='FizzBuzz'/><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>FIzzBuzz with Linq extension methods</title><content type='html'>So if you haven't heard of the FizzBuzz test, it's basically taking in a list of numbers and figuring out if they are divisible, cleanly, by two numbers.  Say you have 3 and 5 and this is your list:
&lt;br /&gt;
&lt;br /&gt;1
&lt;br /&gt;3
&lt;br /&gt;5
&lt;br /&gt;10
&lt;br /&gt;15
&lt;br /&gt;
&lt;br /&gt;
If the number is divisible by 3, then return the Fizz string.  If the number is divisible by 5, return a Buzz string.  If it's divisible by both, then return FizzBuzz.
&lt;br /&gt;
&lt;br /&gt;1
&lt;br /&gt;Fizz
&lt;br /&gt;Buzz
&lt;br /&gt;Buzz
&lt;br /&gt;FizzBuzz
&lt;br /&gt;
Pretty simple and in actuality pretty easy to do with old C# tools, but I wanted to do this with Linq.  With the use of Funcs, Actions, and Linq extension methods it can be done fairly easily.  Technically you can do the whole thing in one line if you don't want to bother with refactoring.
&lt;br /&gt;
&lt;br /&gt;
I have no idea how to format this cleanly, so sorry if the format is confusing.  Basically it is take a list, get the ones you want, concatenate it with the next list.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt; ConvertListOfIntegersWithLinqMethods(&lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; listToConvert, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; fizzNumber, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; buzzNumber)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; result =
  listToConvert
    .Where(WhereBothDivisible(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(&lt;span style="color: rgb(204, 0, 0);"&gt;"FizzBuzz"&lt;/span&gt;))
    .Concat(
  listToConvert
    .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(&lt;span style="color: rgb(204, 0, 0);"&gt;"Buzz"&lt;/span&gt;)))
    .Concat(
  listToConvert
    .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(&lt;span style="color: rgb(204, 0, 0);"&gt;"Fizz"&lt;/span&gt;)))
    .Concat(
  listToConvert
    .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(&lt;span style="color: rgb(204, 0, 0);"&gt;"Nothing"&lt;/span&gt;)));

 return result.ToList().OrderBy(currentItem =&gt; currentItem.Key).ToList();                
}
&lt;/pre&gt;
Using these:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;stati&lt;/span&gt;c &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt; selectKeyValuePair(&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; value)
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; currentItem =&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(currentItem, value);
}
          
&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; WhereBothDivisible(&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; fizzNumber, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; buzzNumber)
{           
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt;  currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;amp;&amp;amp; IsDivisible(currentItem, buzzNumber);
}

&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; WhereFizzDivisable(&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; fizzNumber, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; buzzNumber)
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;amp;&amp;amp; !IsDivisible(currentItem, buzzNumber);
}

&lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; WhereBuzzDivisable(&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; fizzNumber, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; buzzNumber)
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;amp;&amp;amp; IsDivisible(currentItem, buzzNumber);
}

&lt;span style="color: rgb(51, 51, 255);"&gt; private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; WhereNeitherDivisable(&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; fizzNumber, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; buzzNumber)
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;amp;&amp;amp; !IsDivisible(currentItem, buzzNumber);
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-6719865363274655884?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/6719865363274655884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=6719865363274655884' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6719865363274655884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6719865363274655884'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/09/fizzbuzz-with-linq-extension-methods.html' title='FIzzBuzz with Linq extension methods'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1184545382650767815</id><published>2008-09-02T06:41:00.000-07:00</published><updated>2008-09-02T06:53:52.686-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>Beyond the wall</title><content type='html'>So I never gave a solution to&lt;a href="http://byatool.blogspot.com/2008/08/and-then-you-hit-wall.html"&gt; this problem&lt;/a&gt; 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:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;EHH??&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; { currentUser.ID, currentUser.UserName };
&lt;/pre&gt;
Well there is no one solution to this, but there is an easy and clean solution:
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; UserQueryItem
 {
   &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; UserQueryItem ( &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; userID, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; userName )
   {
      UserID = userID;
      UserName = userName;
   }

   &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; UserID { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
   &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; UserName { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
 }
&lt;/pre&gt;
Create a class to hold the information.
&lt;pre&gt;
 &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;UserQueryItem&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;UserQueryItem&lt;/span&gt; { UserID = currentUser.ID, UserName = currentUser.UserName };
&lt;/pre&gt;
Or
&lt;pre&gt;

 &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;UserQueryItem&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;UserQueryItem&lt;/span&gt; (currentUser.ID, currentUser.UserName);
&lt;/pre&gt;
Pretty simple, just a little more work.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1184545382650767815?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1184545382650767815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1184545382650767815' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1184545382650767815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1184545382650767815'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/09/beyond-wall.html' title='Beyond the wall'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1768493485778094200</id><published>2008-08-29T09:48:00.000-07:00</published><updated>2008-08-29T09:53:27.837-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Property Initializers'/><category scheme='http://www.blogger.com/atom/ns#' term='3.0'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>DID U NO?!?!!111</title><content type='html'>So maybe I'm slow, but it just dawned on me that:
&lt;pre&gt;
 &lt;span style="color: rgb(0, 204, 204);"&gt;SomeClass&lt;/span&gt; class = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; SomeClass() { SomeProperty = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt; };
&lt;/pre&gt;
Is the same as:
&lt;pre&gt;
 &lt;span style="color: rgb(0, 204, 204);"&gt;SomeClass&lt;/span&gt; class = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; SomeClass { SomeProperty = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt; };
&lt;/pre&gt;
What?? Look closely.  The "new SomeClass" no longer needs the () if you are using 3.0's property initializers.  Not ground breaking, but interesting.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1768493485778094200?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1768493485778094200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1768493485778094200' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1768493485778094200'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1768493485778094200'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/did-u-no111.html' title='DID U NO?!?!!111'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1665050243924107527</id><published>2008-08-29T05:43:00.000-07:00</published><updated>2008-08-29T06:31:39.106-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Covariance'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Contravariance'/><title type='text'>And now for Contravariance</title><content type='html'>Take the classes in the last post (&lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt;) and assume they are exactly the same in this example.

In Covariance, we learned that whatever variable to you set equal to the return of a method has to be equal in type of larger.  Or in other words, it has to have equal or less functionality.
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  Second&lt;/span&gt; second = ReturnThird();  &lt;span style="color: rgb(0, 102, 0);"&gt;//OK since second has less functionality&lt;/span&gt;

&lt;span style="color: rgb(0, 204, 204);"&gt;  Third&lt;/span&gt; third = ReturnSecond(); &lt;span style="color: rgb(0, 102, 0);"&gt;//BAD since third has more functionality&lt;/span&gt;
&lt;/pre&gt;
Now I think you can guess what Contravariance is, but if you can't it's ok.  Most likely you're a tool just like me.  Contravariance is the movement from small to large meaning that the type must be equal to or larger than.  Following the "in other words" manor, it has to have equal or more functionality.


Now small note before I go on, saying that it has to have more/less functionality can be somewhat dangerous.  After all, &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; could inherit from &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; and add no functionality, but I find this is an easier way to think of it.  I suppose another way of thinking of it is that with Covariance the return type has to have equal or more knowledge.  Meaning, &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; has full knowledge of what &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; is, but &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; has no idea what &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; is.


Anywho, onto some examples.  Say we take the FillX methods and add something to it.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;  private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;void&lt;/span&gt; FillFirst(&lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; firstToFill)
  {
    firstToFill.FirstOutput = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt;;
  }

&lt;span style="color: rgb(51, 51, 255);"&gt;  private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;void&lt;/span&gt; FillSecond(&lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; secondToFill)
  {
    secondToFill.SecondOutput = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt;;
  }

&lt;span style="color: rgb(51, 51, 255);"&gt;  private&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;void&lt;/span&gt; FillThird(&lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; thirdToFill)
  {
    thirdToFill.ThirdOutput = &lt;span style="color: rgb(204, 0, 0);"&gt;""&lt;/span&gt;;
  }
&lt;/pre&gt;
Right off the bat you might notice that if methods allowed Covariance with parameters, you'd be in trouble.  After all, if FillThird allowed parameter covariance, you could pass in a &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; object.  What what that object do with ThirdOutPut?  As things are, you would have a bad day.  Lucky for you, at least if you aren't adamant about wanting Covariance in parameters, this can't happen.


Well shoot, I just gave away the fun of this post.  Oh well, I'll keep going in case you just have more time.
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  Action&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;&amp;gt; fillFirstAction = FillFirst;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //No problems here since FillFirst expects a First&lt;/span&gt;
    
&lt;span style="color: rgb(0, 204, 204);"&gt;  Action&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;&amp;gt; fillSecondAction = FillFirst;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //Still no problems although this may look odd.  But remember, FillFirst&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //just needs an object that : First, it doesn't care if the object&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //has more functionality than first.&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //The FillFirst method uses the FirstOutput property and by inheritance&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //the Second being passed in has said property&lt;/span&gt;
    
&lt;span style="color: rgb(0, 204, 204);"&gt;  Action&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;&amp;gt; fillThirdAction = FillThird;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //Not gonna happen.  The FillThird expects a third or smaller object.  Since&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //Third : Second, third is smaller than second.  Implications?  Look in the &lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //FillThirdMethod&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //The method expects the object to have the ThirdOutput property which means&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;  //Second has to inherit from Third.  We know this to be untrue.&lt;/span&gt;
&lt;/pre&gt;
So basically Contravariance is used with parameters in methods to guarantee the object being passed in has at least the functionality used within the method.



Apparently there was a problem in the lobby so there will be no refreshments served tonight.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1665050243924107527?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1665050243924107527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1665050243924107527' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1665050243924107527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1665050243924107527'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/and-now-for-contravariance.html' title='And now for Contravariance'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-8893735287556312721</id><published>2008-08-28T11:26:00.001-07:00</published><updated>2008-08-28T12:11:19.001-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='Covariance'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Covariance versus Contravariance</title><content type='html'>Ok so I stumbled on to this subject the other day and thought it was worth noting.  Take these simple classes:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; FirstOutput { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; : &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; SecondOutput { &lt;span style="color: rgb(51, 102, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 102, 255);"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; : &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; ThirdOutput { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
}
&lt;/pre&gt;
So from this you can see that &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; inherits &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; which in turns inherits &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;.  By terminology this would mean that &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; is "smaller" than &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; and &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; is "larger" than  both.  Here's an example of Covariance:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Covariance&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Covariance&lt;/span&gt;()
  {
    
      &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;&amp;gt; returnFirstFunc = ReturnFirst;
      &lt;span style="color: rgb(0, 153, 0);"&gt;//This works since the Func has a return type of First&lt;/span&gt;
    

      &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;&amp;gt; returnSecondFunc = ReturnThird;
      &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; secondTest = returnSecondFunc();
      secondTest.FirstOutput = &lt;span style="color: rgb(153, 0, 0);"&gt;"First"&lt;/span&gt;;
      secondTest.SecondOutput = &lt;span style="color: rgb(153, 0, 0);"&gt;"First"&lt;/span&gt;;
      &lt;span style="color: rgb(0, 102, 0);"&gt;//This works since the Func has a return type of Third which is smaller&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //that Second.  Therefore anyone using this Func will expect a Second to&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //be returned and will only use the methods/properties that a Second object&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //would have.  Methods/Properties that Third has by inheritance.&lt;/span&gt;


      &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt;&amp;gt; returnThirdFunc = ReturnSecond;
      &lt;span style="color: rgb(0, 102, 0);"&gt;//THIS WILL NOT WORK&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //Due to Covariance, the return of the method must be equal or smaller&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //that the expected type.  returnThirdFunc expects a Third or smaller object&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //but the ReturnSecond method returns a Second which is not smaller than Third.&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //Afterall, Third : Second&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //Third thirdTest = returnThirdFunc();&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //Is the same as:&lt;/span&gt;
&lt;span style="color: rgb(0, 102, 0);"&gt;       //Third thirdTest = new Second();&lt;/span&gt;
  }

  &lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; ReturnFirst()
  {
      &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; First();
  }

  &lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; ReturnSecond()
  {
      &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; Second();
  }

  &lt;span style="color: rgb(51, 51, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; ReturnThird()
  {
      &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; Third();
  }
}
&lt;/pre&gt;
Basically what this all means is that with return types, the return type must be smaller or equal to the field it's being set to.  When you are dealing with Funcs, the return type must be smaller or equal to the return type for the method it's being set it.  Why is that?  Well think of it like this:


It's your first day on the job and some guy tells you to write something with whatever returnFirstFunc() returns.  Now you have no way to look at the code, so you can only know that it returns &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;.  For all you know, it could return &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;, or &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt;.  So you would do this:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt; someFirst;

someFirst = returnFirstFunc();  &lt;span style="color: rgb(0, 102, 0);"&gt;//Could return anything smaller than First&lt;/span&gt;
someFirst.FirstOutput;  &lt;span style="color: rgb(0, 102, 0);"&gt;//Completely legal and safe&lt;/span&gt;
&lt;/pre&gt;
But would you do this?
&lt;pre&gt;
someFirst.ThirdOutput;
&lt;/pre&gt;
Of course not since you only can assume it is a &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;.  Now let's do this in reverse.  Say from the above example you were allowed to do this:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt;&amp;gt; returnThirdFunc = ReturnSecond;
&lt;/pre&gt;
Could you do this?
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; third;

third = returnThirdFunc();
third.ThirdOutput;
&lt;/pre&gt;
Yeah you can't since the &lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt; type doesn't have the ThirdOutput property.

In short Covariance is the allowance of Smaller types or equal.  If a method returns back Third, then you can use that method for anything that is &lt;span style="color: rgb(0, 204, 204);"&gt;Third&lt;/span&gt; or Smaller (&lt;span style="color: rgb(0, 204, 204);"&gt;Second&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;First&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Object&lt;/span&gt;) but not for something Larger (&lt;span style="color: rgb(0, 204, 204);"&gt;Fourth&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Fifth&lt;/span&gt;, ect).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-8893735287556312721?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/8893735287556312721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=8893735287556312721' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8893735287556312721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8893735287556312721'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/covariance-versus-contravariance.html' title='Covariance versus Contravariance'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-4058217038571687164</id><published>2008-08-26T13:39:00.001-07:00</published><updated>2008-08-26T13:57:52.684-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Mumble Types'/><category scheme='http://www.blogger.com/atom/ns#' term='Anonymous Types'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>And then you hit the wall.</title><content type='html'>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.
&lt;br /&gt;&lt;br /&gt;
Here's the old:
&lt;pre&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt; currentUser.UserID;
&lt;/pre&gt;
Great if I want to select userIDs, but what if I want UserIDs AND UserNames... Easy right?
&lt;pre&gt;
 userList.Select(currentUser =&gt; new { currentUser.ID, currentUser.UserName });
&lt;/pre&gt;
Now this is the old way, but I want the new way... IE the Func way.  Problem is here
&lt;pre&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;EHH??&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt;  new { currentUser.ID, currentUser.UserName };
&lt;/pre&gt;You see, there'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'm screwed.  Now from what I read &lt;a href="http://www.interact-sw.co.uk/iangblog/2008/03/17/lambda-inference"&gt;here &lt;/a&gt; I think I get it. First take the func:
&lt;pre&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;K, T&amp;gt;
&lt;/pre&gt;I have K and T that the compiler has to figure out what they are.  Well it'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's an anonymous type.  So why not just use var?
&lt;pre&gt;  &lt;span style="color: rgb(51, 204, 255);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(51, 204, 255);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt;&amp;gt;
&lt;/pre&gt;Seems easy enough.  I don'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'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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-4058217038571687164?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/4058217038571687164/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=4058217038571687164' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4058217038571687164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4058217038571687164'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/and-then-you-hit-wall.html' title='And then you hit the wall.'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1087664023956350277</id><published>2008-08-26T05:18:00.001-07:00</published><updated>2008-08-26T06:00:02.108-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='Order By'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>When is a field not a field?</title><content type='html'>Ok so for the last few things I'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:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt; SelectFromUserList&amp;lt;K, L&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, K&amp;gt; selectMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, L&amp;gt; orderBy, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList)
{
&lt;span style="color: rgb(0, 204, 204);"&gt;    List&lt;/span&gt;&amp;lt;K&amp;gt; userList = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt;();
    userList = userList.OrderBy(orderBy).Select(selectMethod).ToList();

&lt;span style="color: rgb(51, 51, 255);"&gt;    return&lt;/span&gt; userList;
}
&lt;/pre&gt;
and the call was this:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; newList = userList.Select(selectUser =&gt; selectUser.Name, orderUser =&gt; orderUser.ID, userList);
&lt;/pre&gt;
Let'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'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's a solution to avoid rewriting the lambda expressions:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; selectUserID = currentUser =&gt; currentUser.UserID;
&lt;span style="color: rgb(0, 204, 204);"&gt;  Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;selectUserName = currentUser =&gt; currentUser.UserName;

&lt;span style="color: rgb(0, 204, 204);"&gt;  Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; orderByUserID = currentUser =&gt; currentUser.UserID;
&lt;span style="color: rgb(0, 204, 204);"&gt;  Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; orderByUserName = currentUser =&gt; currentUser.UserName;
&lt;/pre&gt;
What the?  See a while ago I had a method to pass back the expression, but in this case there's nothing to create the expression from (a passed in parameter) since they are really simple expressions.  How would I use them?
&lt;pre&gt;

&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; userIDs;
&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList;
&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; userNames;
  userIDs = SelectFromUserList(selectUserID, orderByUserID, userList);
  userNames = SelectFromUserList(selectUserName, orderByUserID, userList);
&lt;/pre&gt;
Pretty nice huh?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1087664023956350277?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1087664023956350277/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1087664023956350277' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1087664023956350277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1087664023956350277'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/when-is-field-not-field.html' title='When is a field not a field?'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1710475884297052573</id><published>2008-08-25T05:37:00.000-07:00</published><updated>2008-08-25T06:04:52.504-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Order By'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>Adding to the Select</title><content type='html'>So in the last post there was something like this:
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt; SelectUserNameList&amp;lt;K&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, K&amp;gt; selectMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList)
 {
   return userList.Select(selectMethod, userList).ToList();
 }
&lt;/pre&gt;
Called by this:
&lt;pre&gt;
 &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; newList = userList.Select(user =&gt; user.Name, userList);
&lt;/pre&gt;
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 &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, L&amp;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't want to be held down by a specific type.
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt; SelectFromUserList&amp;lt;K, L&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, K&amp;gt; selectMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, L&amp;gt; orderBy, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList)
 {
   &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt; userList = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt;();

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

   &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; userList;
 }
&lt;/pre&gt;
And the new call would be:
&lt;pre&gt;
 &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; newList = userList.Select(selectUser =&gt; selectUser.Name, orderUser =&gt; orderUser.ID, userList);
&lt;/pre&gt;
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:
&lt;br /&gt;&lt;br /&gt;
1) Order the list by user name in a list of users then select the user names.
&lt;br /&gt;&lt;br /&gt;
2) Select a list of user names into a list of strings, order that list by a string.
&lt;br /&gt;&lt;br /&gt;
What if you want to select user names but order by user id?  You can:
&lt;br /&gt;&lt;br /&gt;
1) Order the list by user id and create a list of users then select the user names.
&lt;br /&gt;&lt;br /&gt;
2) Select a list of user names into a list of string and... eh LOSE
&lt;br /&gt;&lt;br /&gt;
Best part about:
&lt;pre&gt;
 userList = userList.Select(selectMethod).OrderBy(orderBy).ToList();
&lt;/pre&gt;
Is that the error is somewhat misleading.  It gives you the "Cannot be inferred by usage" error, not the "Idiot, you can't order a list of Strings by UserID".  So you have to be careful on how you have things in order.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1710475884297052573?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1710475884297052573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1710475884297052573' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1710475884297052573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1710475884297052573'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/adding-to-select.html' title='Adding to the Select'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-7076043861818860587</id><published>2008-08-22T10:46:00.001-07:00</published><updated>2008-08-25T05:36:28.469-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Like'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>Speaking of Select</title><content type='html'>So like Where and other fine Extension methods, Select allows you to either give it a lambda expression like so:
&lt;pre&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; newList = userList.Select(user =&gt; user.Name);
&lt;/pre&gt;
Or
&lt;pre&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; newList = userList.Select(user =&gt; user.ID);
&lt;/pre&gt;
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:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; SelectFromUserList(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; selectMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  return&lt;/span&gt; userList.Select(selectMethod).ToList();
}
&lt;/pre&gt;
Where the select method for a user name would be something like this:
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; nameList = SelectFromUserList(currentUser =&gt; currentUser.UserName, userList);
&lt;/pre&gt;
Sweet... 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.
&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;  List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt; nameList = SelectFromUserList(currentUser =&gt; currentUser.UserID, userList); &lt;span style="color: rgb(0, 153, 0);"&gt;//BOOM&lt;/span&gt;
&lt;/pre&gt;
But wait, you can do that!
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;K&amp;gt; SelectUserNameList&amp;lt;K&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, K&amp;gt; selectMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; userList)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  return&lt;/span&gt; userList.Select(selectMethod).ToList();
}
&lt;/pre&gt;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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-7076043861818860587?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/7076043861818860587/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=7076043861818860587' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7076043861818860587'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7076043861818860587'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/speaking-of-select.html' title='Speaking of Select'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-2267323491328535736</id><published>2008-08-22T07:07:00.000-07:00</published><updated>2008-08-22T07:24:33.314-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>Linq query versus just a Select</title><content type='html'>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...  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...)  Well you have an awful lot to pushout to the front end.  Let's just say it can be a little sluggish.  Lets assume you are just sending the list out, and you've already done all the querying. 

You could do:
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; userList;
&lt;/pre&gt;
Which is what I was doing.  Kind of slow. 

You could do:
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; query = &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; user &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; userList
             &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt;
             {
                UserName = user.UserName,
                UserID = user.ID
             };

 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; query.ToList();
&lt;/pre&gt;
Nothing wrong with that, just a bit verbose for something so simple.

You could do this:
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; userList.Select(currentItem =&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; { Username = currentItem.UserName, UserID = user.ID });
&lt;/pre&gt;
All in one line.  Now I personally like the sql-ish linq expressions, but for this why not fit it into one line?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-2267323491328535736?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/2267323491328535736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=2267323491328535736' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2267323491328535736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2267323491328535736'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/linq-query-versus-just-select.html' title='Linq query versus just a Select'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-47822459735731108</id><published>2008-08-22T05:46:00.001-07:00</published><updated>2008-08-22T06:13:37.012-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Union'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>Union to find if two lists match</title><content type='html'>So let's say you have two lists you want to compare to see if they hold the same items, but the items are not equal reference.  Now, if you are comparing two lists    that have unique values to compare, Union is perfect.
&lt;br /&gt;
&lt;br /&gt;
List 1  { 1, 2, 3, 4, 5 }
&lt;br /&gt;
List 2 { 2, 1, 4, 3, 5 }
&lt;br /&gt;
&lt;br /&gt;
As you can see, there are no repeated values in these two lists.  Easy way to figure out if all the values are the same in the two lists:
&lt;pre&gt;
  &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; query = (&lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; first &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; firstList
            &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; first).Union(&lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; second &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; secondlist
                                &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; second);

  &lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(query.Count() == first.Count());&lt;/pre&gt;
Why does this work?  Union combine the two lists, removing any duplicates.  So if everything goes correctly, the count of the new list has to match the count of either of the olds lists.  After all 5 pairs of duplicate items gets reduced to a list of 5.  Now, if there is anything different between the lists the count will get screwed.  Why?  Because even one difference will cause an extra item to show up in the list.
&lt;br /&gt;
&lt;br /&gt;
List 1  { 1, 2, 3, 4, 5 }
&lt;br /&gt;
List 2 { 1, 2, 3, 4, 6 }
&lt;br /&gt;
Union { 1, 2, 3, 4 , 5, 6 }
&lt;br /&gt;
&lt;br /&gt;
And it will only get worse for every mismatch.
&lt;br /&gt;
&lt;br /&gt;
Real worldish example:
&lt;pre&gt;    &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; query = (&lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; user &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; userListFirst
              &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; user.UserID).Union(&lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; secondUser &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; userListSecond
                                       &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; second.UserID);

   &lt;span style="color: rgb(0, 204, 204);"&gt; Assert&lt;/span&gt;.IsTrue(query.Count() == userListFirst.Count());&lt;/pre&gt;
Bonus points if you can figure out why this would fail at times.  Actually, I already told you...
&lt;br /&gt;
&lt;br /&gt;
UsInGS
&lt;pre&gt;
  &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
  &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-47822459735731108?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/47822459735731108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=47822459735731108' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/47822459735731108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/47822459735731108'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/union-to-find-if-two-lists-match.html' title='Union to find if two lists match'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-5976984788684392211</id><published>2008-08-19T13:29:00.000-07:00</published><updated>2008-08-19T13:39:02.216-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='action'/><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>Life is fun when you are slow</title><content type='html'>&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;void&lt;/span&gt; IfTrueRunMethod(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; trueMethod, &lt;span style="color: rgb(0, 204, 204);"&gt;Action&lt;/span&gt; action)
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt;(trueMethod())
  {
    action();
  }
}&lt;/pre&gt;
Just something I made for the hell of it to remove:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt;(someClass != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; someClass.Property == &lt;span style="color: rgb(204, 0, 0);"&gt;"hi"&lt;/span&gt;)
{
  SomeMethod();
}&lt;/pre&gt;
This can be reduced to one line... yay!
&lt;pre&gt;
IfTrueRunMethod(() =&amp;gt; { someClass != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; someClass.Property == &lt;span style="color: rgb(204, 0, 0);"&gt;"hi"&lt;/span&gt; }, () =&amp;gt; SomeMethod());&lt;/pre&gt;
You could even transform the first part into a method if you want:
&lt;pre&gt;
IfTrueRunMethod(() =&amp;gt; TrueMethod(someClass), () =&amp;gt; SomeMethod());&lt;/pre&gt;
Weeeee!
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-5976984788684392211?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/5976984788684392211/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=5976984788684392211' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5976984788684392211'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5976984788684392211'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/public-void-iftruerunmethod-func.html' title='Life is fun when you are slow'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-936157245833663811</id><published>2008-08-14T11:22:00.000-07:00</published><updated>2008-08-14T11:36:14.463-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Enumeration'/><title type='text'>Another silly method just for you</title><content type='html'>So what if you want the names and values from an Enum, but wanted them in dictionary form.  Well shoot, a little bit of linq and little bit of that and you got this:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; ConvertEnumToDictionary&amp;lt;K&amp;gt;()
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(K).BaseType != &lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Enum&lt;/span&gt;))
 {
   &lt;span style="color: rgb(51, 51, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;InvalidCastException&lt;/span&gt;();
 }

 &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Enum&lt;/span&gt;.GetValues(&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(K)).Cast&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt;().ToDictionary(currentItem =&amp;gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Enum&lt;/span&gt;.GetName(&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(K), currentItem));
}
&lt;/pre&gt;As you might see, pretty simple. 

Ok so ToDictionary is kind of odd looking maybe, but really isn't that big of a deal.  Basically it assumes the items in the list are the value, but you need to tell it what the key is.  In this case, the int values for the enum are the value for the dictionary where the name that matches said int value will become the key for the dictionary.

So basically, get the values for the enum. Turn that into an IEnumerable list of Int32, create a Dictionary from that.

USINGS!!111
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-936157245833663811?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/936157245833663811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=936157245833663811' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/936157245833663811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/936157245833663811'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/another-silly-method-just-for-you.html' title='Another silly method just for you'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-4486107604088171501</id><published>2008-08-12T05:49:00.000-07:00</published><updated>2008-08-12T06:16:50.801-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dynamic Controls'/><category scheme='http://www.blogger.com/atom/ns#' term='Javascript'/><title type='text'>You can do that in Javascript? Part 2</title><content type='html'>Creating divs on the fly and assigning methods:

As I have been working with Script Controls lately, I've been forces to learn more about javascript.... yeah I know, bleh.  However, in my learnin' I've actually been forced to like Javascript.... yeah I know, bleh.

Well one this I was doing was tranfering a front end control to a Script Control.  Basically I am building an Auto Complete control that is using the Ajax.dll AjaxMethod stuff.  Thus skipping the need for web services that the Ajax Control AutoComplete needs.  Anyhow, the reason I am saying this is that it gets a list of Users and dynamically creates a list of divs that change color when hovered over and fill in a textbox when selected.  Originally I was doing this by creating the html needed, then appending the innerHTML property on the container div.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;function&lt;/span&gt; buildSelectableDiv(currentCount, innerText, textboxName, parentDiv)
{
   &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; defaultClass;
   &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; divChild;
   &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; divToAdd;
   &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; picker;

   &lt;span style="color: rgb(0, 153, 0);"&gt;//create the parent div&lt;/span&gt;
   divToAdd = document.createElement(&lt;span style="color: rgb(153, 0, 0);"&gt;'div'&lt;/span&gt;);
   &lt;span style="color: rgb(0, 153, 0);"&gt;//set the id of the div&lt;/span&gt;
   divToAdd.setAttribute(&lt;span style="color: rgb(153, 0, 0);"&gt;'name'&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;'divNames'&lt;/span&gt; + currentCount);

   &lt;span style="color: rgb(0, 153, 0);"&gt;//Create child div&lt;/span&gt;
   divChild = document.createElement(&lt;span style="color: rgb(153, 0, 0);"&gt;'div'&lt;/span&gt;);
   &lt;span style="color: rgb(0, 153, 0);"&gt;//getting the child ready&lt;/span&gt;
   divChild.setAttribute(&lt;span style="color: rgb(153, 0, 0);"&gt;'name'&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;'divNamesChild'&lt;/span&gt; + currentCount);

   &lt;span style="color: rgb(0, 153, 0);"&gt;//Add child to new parent&lt;/span&gt;
   divToAdd.appendChild(divChild);

   &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; divToAdd;
}
&lt;/pre&gt;
And there you go.  Creating a div and adding div to it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-4486107604088171501?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/4486107604088171501/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=4486107604088171501' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4486107604088171501'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4486107604088171501'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/you-can-do-that-in-javascript-part-2.html' title='You can do that in Javascript? Part 2'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-5275315460381825700</id><published>2008-08-12T05:44:00.000-07:00</published><updated>2008-08-12T05:48:58.895-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='Anonymous Methods'/><title type='text'>You can do that in Javascript?</title><content type='html'>So found out yesterday that anonymous methods exist in javascript.  Who knew?
&lt;pre&gt;
 divToAdd.onmouseover = &lt;span style="color: rgb(51, 51, 255);"&gt;function&lt;/span&gt;() { picker.changeStyles(divToAdd, &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;); };
&lt;/pre&gt;Where picker.changeStyles is a method on a class.  Javascript is starting to grow on me.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-5275315460381825700?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/5275315460381825700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=5275315460381825700' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5275315460381825700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5275315460381825700'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/08/you-can-do-that-in-javascript.html' title='You can do that in Javascript?'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1658151966688218090</id><published>2008-07-24T10:06:00.000-07:00</published><updated>2008-07-24T10:19:03.034-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='TryParse'/><category scheme='http://www.blogger.com/atom/ns#' term='Session'/><category scheme='http://www.blogger.com/atom/ns#' term='Generics'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Yeah SessionTryParse</title><content type='html'>So I got tired of seeing If Session["SomeKey"] != null... blah blah blah and thought it would be a semi worthwhile task to create a TryParse method because I'm bad like that.  Not a Bad Enough Dude to Save the President, but bad.
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt; SessionTryParse&amp;lt;K&amp;amp;gt;&lt;span style="color: rgb(0, 204, 204);"&gt;(HttpSessionState &lt;/span&gt;session, &lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;key, &lt;span style="color: rgb(51, 51, 255);"&gt;out &lt;/span&gt;K itemToSet) &lt;span style="color: rgb(51, 51, 255);"&gt;where &lt;/span&gt;K : &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt;
 {
     itemToSet = &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;;

     &lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt; (session[key] != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
     {
        itemToSet = session[key] &lt;span style="color: rgb(51, 51, 255);"&gt;as&lt;/span&gt; K;
     }

     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; itemToSet != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;;
 }
&lt;/pre&gt;
And for structures... which I have to cheat and only allow them to be nullable.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt; SessionTryParse&amp;lt;K&amp;gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;HttpSessionState&lt;/span&gt; session, &lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;key, &lt;span style="color: rgb(51, 51, 255);"&gt;out &lt;/span&gt;K? itemToSet) &lt;span style="color: rgb(51, 51, 255);"&gt;where &lt;/span&gt;K : &lt;span style="color: rgb(51, 51, 255);"&gt;struct&lt;/span&gt;
{
     itemToSet = &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;;

     &lt;span style="color: rgb(51, 51, 255);"&gt;if &lt;/span&gt;(session[key] != &lt;span style="color: rgb(51, 51, 255);"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; session[key] &lt;span style="color: rgb(51, 51, 255);"&gt;is&lt;/span&gt; K)
     {
        itemToSet = (K)session[key];
     }

     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; itemToSet != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;;
}
&lt;/pre&gt;
The idea is simple, pull in the Session, the needed Session key, and something to throw the value in.  After that, just check to see if the Session + Key is null and if Session + Key is the same type of said something.  That's how it's done Detroit greater metropolitan area style... punk.

USE THIS:
&lt;pre&gt;
   &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
   &lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Web.SessionState;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1658151966688218090?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1658151966688218090/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1658151966688218090' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1658151966688218090'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1658151966688218090'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/yeah-sessiontryparse.html' title='Yeah SessionTryParse'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-2450991585884716889</id><published>2008-07-22T08:52:00.001-07:00</published><updated>2008-07-22T09:04:12.188-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Like'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Contains'/><title type='text'>Like versus Contains</title><content type='html'>So have to figure this one out.  Say you have:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; WhereLikeFirstNameLastNameUserName(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;name)
{
 &lt;span style="color: rgb(51, 51, 255);"&gt; return &lt;/span&gt;currentUser =&amp;gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.UserName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;)
  || &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.FirstName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;)
  || &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.LastName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;);
}&lt;/pre&gt;And
&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression &lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; WhereLikeFirstNameLastNameUserNameWithoutLike(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;name)
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;currentUser =&amp;gt; currentUser.UserName.Contains(name)
  || currentUser.FirstName.Contains(name)
  || currentUser.LastName.Contains(name);
}&lt;/pre&gt;
The first one should look familiar, its part of the "dynamic" linq stuff I've been posting.  Now guess which one gives me this SQL when profiled:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;exec &lt;/span&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;sp_executesql&lt;/span&gt; N
&lt;span style="color: rgb(255, 0, 0);"&gt;'SELECT&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[UserID],&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[FirstName],&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[LastName],&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[Password],&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[UserName],&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[UserTypeID]&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt;FROM&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [dbo].[User] AS [t0]&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt;WHERE&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; ([t0].[UserName] LIKE @p0)&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt;OR&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; ([t0].[FirstName] LIKE @p1)&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt;OR&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; ([t0].[LastName] LIKE @p2)&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt;ORDER BY&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; [t0].[UserName]'&lt;/span&gt;,

N&lt;span style="color: rgb(255, 0, 0);"&gt;'@p0 varchar(3),&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; @p1 nvarchar(3),&lt;/span&gt;
&lt;span style="color: rgb(255, 0, 0);"&gt; @p2 nvarchar(3)'&lt;/span&gt;,
 @p0=&lt;span style="color: rgb(255, 0, 0);"&gt;'%s%'&lt;/span&gt;,
 @p1=N&lt;span style="color: rgb(255, 0, 0);"&gt;'%s%'&lt;/span&gt;,
 @p2=N&lt;span style="color: rgb(255, 0, 0);"&gt;'%s%'&lt;/span&gt;&lt;/pre&gt;
If you answered both, you are correct or you looked ahead for the answer and therefore are a tool.  Now which do you think that...
&lt;pre&gt;
query.ToList()&lt;/pre&gt;
Produces the correct list?  If you answered Contains, then you are correct again.  If you are a tool, you probably looked ahead again...

Why is this?  I HAVE NO IDEA...  Something I have to look into for sure.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-2450991585884716889?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/2450991585884716889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=2450991585884716889' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2450991585884716889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2450991585884716889'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/like-versus-contains.html' title='Like versus Contains'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-8914256451406138651</id><published>2008-07-18T10:46:00.000-07:00</published><updated>2008-07-18T10:49:35.463-07:00</updated><title type='text'>Backtrack</title><content type='html'>Couple posts ago I said "It took me a minute to figure out how this works. I thought it was somehow in need of the property name to order by, but in reality it is looking for a list of strings to order by." when I was talking about how to create an order by expression.  I thought about it more, and that doesn't make sense as far as the sql goes.  What I was saying would mean it would get the list of strings and sort based on them.  What I think actually happens is it takes the Expression (Hense the name) and derives the order by from the epression.

Say I want tell the thing I will want to sort by user.UserName, it will take that expression and translate it into ORDER BY someAlias.UserName.  Magic.  I would like to know how it does this.  Reflection and a ton of it I would assume.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-8914256451406138651?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/8914256451406138651/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=8914256451406138651' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8914256451406138651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8914256451406138651'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/backtrack.html' title='Backtrack'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-8527876930094351387</id><published>2008-07-18T10:22:00.000-07:00</published><updated>2008-07-18T10:36:39.891-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='Expression'/><category scheme='http://www.blogger.com/atom/ns#' term='closures'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>I'm starting to worry about myself</title><content type='html'>So the "dynamic" linq query so far isn't just enough to stop.  Oh no, now that I can have methods pass back expressions, what about a dictionary of order by expressions to allow an even more dynamic feel?  Huh?  How's about that kids?  I hate myself too.
&lt;pre&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//Enum created for the dictionary key&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public enum&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice&lt;/span&gt;
{
  FirstName,
  LastName,
  UserName
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//dictionary of expressions&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice&lt;/span&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;  GetOrderByList()
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  if&lt;/span&gt;(orderByList == &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
  {
    orderByList = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;OrderByChoice,&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;();

    orderByList.Add(&lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice&lt;/span&gt;.FirstName, currentUser =&amp;gt; currentUser.FirstName);
    orderByList.Add(&lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice&lt;/span&gt;.LastName, currentUser =&amp;gt; currentUser.LastName);
    orderByList.Add(&lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice&lt;/span&gt;.UserName, currentUser =&amp;gt; currentUser.UserName);
  }
         
&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;orderByList;
}   
&lt;/pre&gt;
That's right, I know.  Silly, but I think it's cool.  Now mind you, I could have methods that return expressions instead of the expressions themselves, but I wanted to show the expressions.

And for the method call:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt;&lt;span style="color: rgb(0, 204, 204);"&gt; &lt;/span&gt;GetUserList(&lt;span style="color: rgb(0, 204, 204);"&gt;OrderByChoice &lt;/span&gt;orderBy)
{
&lt;span style="color: rgb(0, 204, 204);"&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;&lt;/span&gt;GetUserList(currentUser =&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;, GetOrderByList()[orderBy]).ToList();
}
&lt;/pre&gt;
And now you have an even more dynamicish call.

I FORGOT THE USINGS!!!!
&lt;pre&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Collections.Generic;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq;
 &lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq.Expressions;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-8527876930094351387?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/8527876930094351387/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=8527876930094351387' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8527876930094351387'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8527876930094351387'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/im-starting-to-worry-about-myself.html' title='I&apos;m starting to worry about myself'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-6400636075995798135</id><published>2008-07-17T15:53:00.000-07:00</published><updated>2008-07-18T05:37:46.269-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='closures'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><title type='text'>And some days I love programming</title><content type='html'>This is the newest edition of Linq madness.  So I added an OrderBy to my lame dynamic query thing.

&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserList(&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; whereClause, &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt; orderBy)
{
&lt;span style="color: rgb(51, 102, 255);"&gt;    &lt;span style="color: rgb(51, 51, 255);"&gt;var &lt;/span&gt;&lt;/span&gt;query = (&lt;span style="color: rgb(51, 51, 255);"&gt;from &lt;/span&gt;user &lt;span style="color: rgb(51, 51, 255);"&gt;in &lt;/span&gt;GetDataContext().Users
      &lt;span style="color: rgb(51, 51, 255);"&gt;       select &lt;/span&gt;user).Where(whereClause).OrderBy(orderBy);

&lt;span style="color: rgb(51, 51, 255);"&gt;    return &lt;/span&gt;query;
}&lt;/pre&gt;
Now you will notice there is a new Expression in town and it's name isn't Reggie Hammond.  It is my order by Expression which uses a &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;.  It took me a minute to figure out how this works.  I thought it was somehow in need of the property name to order by, but in reality it is looking for a list of strings to order by.  Simple.  I could do something like: (Ignore the WhereLikeFirstNameLastNameUserName for now)
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;    return &lt;/span&gt;GetUserList
           (
            WhereLikeFirstNameLastNameUserName(name),
            currentUser =&amp;gt; currentUser.UserName
           ).ToList();&lt;/pre&gt;
But that's boring.  I want to be able to pass a method that would give the correct string to orderby somewhat cleaner.  In comes a method that returns an Expression. OooOoOOo.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt; SortOnUserName()
{
&lt;span style="color: rgb(51, 51, 255);"&gt;    return &lt;/span&gt;currentUser =&amp;gt; currentUser.UserName;
}&lt;/pre&gt;
So now it looks like:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;User&amp;gt;&lt;span style="color: rgb(0, 204, 204);"&gt; &lt;/span&gt;GetUserListByLikeName(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;name)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;   return &lt;/span&gt;GetUserList
          (
           WhereLikeFirstNameLastNameUserName(name),
           SortOnUserName()
          ).ToList();
}&lt;/pre&gt;
Cleaner... Now what is WhereLikeFirstNameLastNameUserName? Simple, that is my where Expression just being returned in this method:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; WhereLikeFirstNameLastNameUserName(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;name)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;   return &lt;/span&gt;currentUser =&amp;gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.UserName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;)
   || &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.FirstName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;)
   || &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like(currentUser.LastName, &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt; + name + &lt;span style="color: rgb(204, 0, 0);"&gt;"%"&lt;/span&gt;);
}
&lt;/pre&gt;

By the way, &lt;span style="color: rgb(0, 204, 204);"&gt;SqlMethods&lt;/span&gt;.Like is just a built in method used only with Linq to Sql. Probably shouldn't have used it in this example but oh well.  Live with it.

OH NO NOT THE USINGS!!
&lt;pre&gt;
&lt;span style="color: rgb(51, 102, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Data.Linq.SqlClient;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq.Expressions;
&lt;/pre&gt;

&lt;span style="color: rgb(0, 204, 204);"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-6400636075995798135?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/6400636075995798135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=6400636075995798135' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6400636075995798135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/6400636075995798135'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/and-some-days-i-love-programming.html' title='And some days I love programming'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-4033171866075418750</id><published>2008-07-17T12:22:00.000-07:00</published><updated>2008-07-18T05:28:43.775-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='stupid'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>Quick Linq thing... String to Number String</title><content type='html'>Really stupid little thing I came up with today... but I wanted to use Linq to strip any non number from a string and return the string with only numbers.  I told you this was a stupid little thing.

&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;CreateNumberOnlyString(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;textToCheck)
{
  &lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;returnText;
               
  &lt;span style="color: rgb(51, 51, 255);"&gt;var &lt;/span&gt;queryForIntegers = &lt;span style="color: rgb(51, 51, 255);"&gt;from &lt;/span&gt;currentChar &lt;span style="color: rgb(51, 51, 255);"&gt;in &lt;/span&gt;textToCheck
                         &lt;span style="color: rgb(51, 51, 255);"&gt;where &lt;/span&gt;Char.IsNumber(currentChar)
                         &lt;span style="color: rgb(51, 51, 255);"&gt;select &lt;/span&gt;currentChar;
               
  returnText = &lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;String(queryForIntegers.ToArray());
               
  &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;returnText;
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-4033171866075418750?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/4033171866075418750/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=4033171866075418750' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4033171866075418750'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/4033171866075418750'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/quick-linq-thing-string-to-number.html' title='Quick Linq thing... String to Number String'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-5954684463947760721</id><published>2008-07-17T09:30:00.000-07:00</published><updated>2008-07-18T05:27:51.387-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='closures'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Lambda'/><title type='text'>I likz demz linq</title><content type='html'>So today I had this weird need to try creating a sort of dynamic linq thing in which I could query a User list but not have to write a Linq query for every method.  I just wanted to make a method that would return the correct query that I wanted based on a simple "dynamic" where clause.  Well part of the reason this should be easy is if you look at the Where extension method on an IEnumerable collection, you will see it needs a &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;T, K&amp;gt; where T is the type of the items in the list.  So ding!, create a method that takes in a &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; and puts it in them thar where clause. Then you could easily use a lambda expression to create the Func. At first you might thing something like this:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserList(&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt; whereClause)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  var &lt;/span&gt;query = &lt;span style="color: rgb(51, 51, 255);"&gt;from &lt;/span&gt;user &lt;span style="color: rgb(51, 102, 255);"&gt;in &lt;/span&gt;Users
          &lt;span style="color: rgb(51, 51, 255);"&gt;  where &lt;/span&gt;whereClause(user)
            &lt;span style="color: rgb(51, 51, 255);"&gt;select &lt;/span&gt;user;

&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;query;
}

&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserListByUserName(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;userName)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;GetUserList(currentUser =&amp;gt; currentUser.UserName == userName).ToList();
}
&lt;/pre&gt;
AND YOU WOULD BE WRONG! I only know this because... eh... I saw a friend of mine try this and it failed.  Fact is, the Linq where doesn't take in a Func, but rather it expects and Expression.  So no big deal right?  Slap on an expression and go... except you can't keep the same syntax.  (Trust me, my... friend tried)  Being the absolute genius that I am, I offered my friend a solution, and it's slightly ugly:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserList(&lt;span style="color: rgb(0, 204, 204);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean&lt;/span&gt;&amp;gt;&amp;gt; whereClause)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  var &lt;/span&gt;query = (&lt;span style="color: rgb(51, 51, 255);"&gt;from &lt;/span&gt;user &lt;span style="color: rgb(51, 51, 255);"&gt;in &lt;/span&gt;Users
          &lt;span style="color: rgb(51, 51, 255);"&gt;  select &lt;/span&gt;user).Where(whereClause);

&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;query;
}
&lt;/pre&gt;
Could be used like:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserList()
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;GetUserList(currentUser =&amp;gt; &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;).ToList();
}
&lt;/pre&gt;
Or
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetUserListByFirstName(&lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;firstName)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  return &lt;/span&gt;GetUserList(currentUser =&amp;gt; currentUser.FirstName == firstName).ToList();
}
&lt;/pre&gt;
Yeah, not the prettiest of things, but it is what it is.  I'm not even sure this is useful yet.  Still screwing around with it.  Does cut down on code for easy queries.


AND NOW FOR THE ASSEMBLIES!
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: rgb(51, 51, 255);"&gt;using &lt;/span&gt;System.Linq.Expressions;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-5954684463947760721?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/5954684463947760721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=5954684463947760721' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5954684463947760721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/5954684463947760721'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/i-likz-demz-linq.html' title='I likz demz linq'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1659666087889332862</id><published>2008-07-14T13:55:00.001-07:00</published><updated>2008-07-15T10:47:02.055-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='closures'/><category scheme='http://www.blogger.com/atom/ns#' term='func'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>I ain't gettin' paid enough for this...</title><content type='html'>Remember when I said I'm not exactly great with programming terms?  Well if I didn't, I am saying it now.  So I saw the word "closures" today and had no idea what this was.  All excited about something new, I found out that closures is just another word for anonymous methods, although it may include stuff outside that scope but I'm not sure.  Anyhow, although this seemed to be yet another attack of the stupid I stumbled across something &lt;a href="http://blogs.msdn.com/abhinaba/archive/2005/08/08/448939.aspx"&gt;here.&lt;/a&gt;  Basically what caught me is how value types are handled with Funcs.  Take this method:
&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Linq;

&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; ReturnIntegerWithinFuncReturningMethod(&lt;span style="color: rgb(0, 204, 204);"&gt;WhichMethod&lt;/span&gt; methodToReturn)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&lt;/span&gt; integerToIncrement;
&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; returnMethod;
  
&lt;span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;/span&gt;&lt;/span&gt;integerToIncrement = 0;
&lt;span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;/span&gt;&lt;/span&gt;returnMethod = &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;;
  
&lt;span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;switch&lt;/span&gt;(methodToReturn)
&lt;span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;  &lt;/span&gt;&lt;/span&gt;{
&lt;span style="color: rgb(51, 51, 255);"&gt;  case&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;WhichMethod&lt;/span&gt;.AddOne:
   returnMethod = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt;(() =&amp;gt; { &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; integerToIncrement += 1;} );
 &lt;span style="color: rgb(51, 51, 255);"&gt;  break&lt;/span&gt;;
&lt;span style="color: rgb(51, 51, 255);"&gt;  case&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;WhichMethod&lt;/span&gt;.AddTwo:
   returnMethod = &lt;span style="color: rgb(51, 51, 255);"&gt;new Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt;(() =&amp;gt; { &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; integerToIncrement += 2; });
 &lt;span style="color: rgb(51, 51, 255);"&gt;  break&lt;/span&gt;;
}
  
&lt;span style="color: rgb(51, 51, 255);"&gt; return&lt;/span&gt; returnMethod;
}
&lt;/pre&gt;

Let's say we test it like this:

&lt;pre&gt;
&lt;span style="color: rgb(0, 204, 204);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt;&amp;gt; returnedMethod;
&lt;span style="color: rgb(0, 204, 204);"&gt;Int32&lt;/span&gt; integerToCheck;

returnedMethod = ReturnIntegerWithinFuncReturningMethod(&lt;span style="color: rgb(0, 204, 204);"&gt;WhichMethod&lt;/span&gt;.AddOne);
integerToCheck = returnedMethod();
&lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(integerToCheck == 1, &lt;span style="color: rgb(204, 0, 0);"&gt;"Actual value is:"&lt;/span&gt; + integerToCheck.ToString());

integerToCheck = returnedMethod();
&lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(integerToCheck == 2, &lt;span style="color: rgb(204, 0, 0);"&gt;"Actual value is:"&lt;/span&gt; + integerToCheck.ToString());&lt;/pre&gt;

These both pass.  How does that make sense?  You would think that it would return 1 both times since from first glance integerToIncrement lives outside the actual &lt;span style="color: rgb(0, 0, 0);"&gt;Func&lt;/span&gt; that is returned.  You would think from this that Int32 is a reference type.  When a value type is "attached" to a Func like this one is, apparently it keeps a reference to it and therefore the original Int32 is updated every time the Func is called.  Something to remember.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1659666087889332862?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1659666087889332862/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1659666087889332862' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1659666087889332862'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1659666087889332862'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/i-aint-gettin-pain-enough-for-this.html' title='I ain&apos;t gettin&apos; paid enough for this...'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1451856489614951908</id><published>2008-07-10T13:26:00.000-07:00</published><updated>2008-07-15T10:47:43.606-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Exception'/><title type='text'>Guard Clause Method</title><content type='html'>So I was introduced to the idea of a "guard clause" in methods when I started my latest job.  The idea is to make sure that all the passed in parameters of a method are, for example, not null.  If they are, just throw an exception.  This is what I have been doing:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt;(someField == &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
{
&lt;span style="color: rgb(51, 51, 255);"&gt;throw new&lt;/span&gt; ArgumentNullException();
}
&lt;/pre&gt;
Well just recently I was somewhat schooled in the idea of removing if statements and creating simple methods in their steed.   Now this isn't always a need, but in complex (ie annoying) nested ifs, it might help to clean things up.  So in light of this, I came up with this method:
&lt;pre&gt;

&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: rgb(51, 51, 255);"&gt;using&lt;/span&gt; System.Reflection;

&lt;span style="color: rgb(51, 51, 255);"&gt;private void&lt;/span&gt; ThrowExceptionIfNull&amp;lt;TException, TObject&amp;gt;(TObject objectToCheck, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt; message)
&lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; TException : &lt;span style="color: rgb(0, 204, 204);"&gt;Exception &lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; TObject : &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt;(objectToCheck == &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
  {
      TException exception;

      exception = &lt;span style="color: rgb(0, 204, 204);"&gt;Activator&lt;/span&gt;.CreateInstance&amp;lt;TException&amp;gt;();
      &lt;span style="color: rgb(0, 153, 0);"&gt;//Set the message field on System.Exception since the property is Get only&lt;/span&gt;
      &lt;span style="color: rgb(51, 51, 255);"&gt;if&lt;/span&gt;(message != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
      {
          &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt; fieldInfoList;
&lt;span style="color: rgb(0, 204, 204);"&gt;           FieldInfo &lt;/span&gt;neededInfo;
  
          fieldInfoList = &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt;(&lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(0, 204, 204);"&gt;Exception&lt;/span&gt;).GetFields(&lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.Public));
          neededInfo = fieldInfoList.Find(currentItem =&amp;gt; &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;.Compare(currentItem.Name, &lt;span style="color: rgb(204, 0, 0);"&gt;"_message"&lt;/span&gt;) == 0);
          &lt;span style="color: rgb(0, 153, 0);"&gt;//make sure that the message field is still called _message, otherwise&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;           //forget the message.&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;           if&lt;/span&gt;(neededInfo != &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
          {
              neededInfo.SetValue(exception, message);
          }
      }
      &lt;span style="color: rgb(51, 51, 255);"&gt;throw &lt;/span&gt;exception;
  }
}
&lt;/pre&gt;
And the use:
&lt;pre&gt;
ThrowExceptionIfNull&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;ArgumentNullException&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;(someParameter, "someParameter");
&lt;/pre&gt;
Couple things to keep in mind:
&lt;ul&gt;
&lt;li&gt;
This uses reflection to find the message field since there is no set accessor to the Message property.  This could be dangerous if Microsoft decides to rename the _message field.
&lt;/li&gt;
&lt;li&gt;
The use of reflection would usually include the caching of the field info since you don't want to keep using reflection for the same class every time. In this case, you're throwing an exception so everything is done anyhow.  The cost advantage of a cache is pointless.
&lt;/li&gt;
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1451856489614951908?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1451856489614951908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1451856489614951908' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1451856489614951908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1451856489614951908'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/guard-clause-method.html' title='Guard Clause Method'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1503337229799772475</id><published>2008-07-09T09:59:00.000-07:00</published><updated>2008-07-15T10:48:15.684-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Clause'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>Sub Select and IN Clause faking</title><content type='html'>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'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 the records in one list are the same as the other.  I'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 &lt;span style="color: rgb(51, 51, 255);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt;.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; query = &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; listOneUser &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; listOne
            &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt;
            !(
                &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; listTwoUser &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; listTwo
                &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; listTwoUser.UserID
            ).Contains(listOneUser.UserID)
              &lt;span style="color: rgb(51, 51, 255);"&gt;select&lt;/span&gt; listOneUser;
&lt;/pre&gt;
I select all the IDs from the second list and then see if the first list has any users that don'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't bullet proof, just a way to show the kind of IN clause.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1503337229799772475?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1503337229799772475/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1503337229799772475' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1503337229799772475'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1503337229799772475'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/sub-select-and-in-clause-faking.html' title='Sub Select and IN Clause faking'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-7754834995893640723</id><published>2008-07-09T06:20:00.000-07:00</published><updated>2008-07-15T10:48:39.399-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Join'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Anonymous Types'/><title type='text'>Joining By Anonymous Types</title><content type='html'>Just found this out yesterday so I thought I would post and pass on to all two of you reading this.




Suppose you have a User table and a Contacts table and you wanted to find all the users that match up with the contacts table.  Now suppose there is no direct correlation. What to do?  You could do something really brilliant by joining the tables together on FirstName and LastName, because we all know that there will always only be one John Smith in either table.  Screw you, I couldn't think of a better example at the time.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; GetAllUsersWithMatchingContactInformationUsingJoin()
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;User&lt;/span&gt;&amp;gt; foundUsers;
          
  &lt;span style="color: rgb(51, 51, 255);"&gt;var&lt;/span&gt; query = &lt;span style="color: rgb(51, 51, 255);"&gt;from&lt;/span&gt; user &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; dataContext.Users
              &lt;span style="color: rgb(51, 51, 255);"&gt;join &lt;/span&gt;contact in dataContext.Contacts &lt;span style="color: rgb(51, 51, 255);"&gt;on new&lt;/span&gt; {user.FirstName, user.LastName } &lt;span style="color: rgb(51, 51, 255);"&gt;equals new&lt;/span&gt; { contact.FirstName, contact.LastName }
              select user;
          
              foundUsers = query.ToList();
          
  &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; foundUsers;
}
&lt;/pre&gt;
As you can see here:
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;join &lt;/span&gt;contact &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; dataContext.Contacts &lt;span style="color: rgb(51, 51, 255);"&gt;on new&lt;/span&gt; {user.FirstName, user.LastName } &lt;span style="color: rgb(51, 51, 255);"&gt;equals new&lt;/span&gt; { contact.FirstName, contact.LastName }
&lt;/pre&gt;
You can create a type on the fly and then compare it to another.  I thought that was interesting.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-7754834995893640723?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/7754834995893640723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=7754834995893640723' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7754834995893640723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7754834995893640723'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/joining-by-anonymous-types.html' title='Joining By Anonymous Types'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-9067159910767682601</id><published>2008-07-07T11:58:00.001-07:00</published><updated>2008-07-18T05:29:20.040-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Order By'/><category scheme='http://www.blogger.com/atom/ns#' term='stupid'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><category scheme='http://www.blogger.com/atom/ns#' term='Select'/><title type='text'>I are tupid</title><content type='html'>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't ask why, just go with it.  You could do this:




Where someDictionary is Dictionary&amp;lt;Type, string&amp;gt; :
&lt;pre&gt;

&lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt; dataSource = new &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt;(someDictionary);
dataSource.Sort(&lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;SomeComparer&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;String&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: rgb(255, 0, 0);"&gt;"Value"&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;));
&lt;/pre&gt;

To:

&lt;pre&gt;
&lt;span style="color: rgb(51, 102, 255);"&gt;var&lt;/span&gt; query = &lt;span style="color: rgb(51, 51, 255);"&gt;from &lt;/span&gt;keyValuePair &lt;span style="color: rgb(51, 51, 255);"&gt;in&lt;/span&gt; someDictionary
          &lt;span style="color: rgb(51, 51, 255);"&gt;orderby &lt;/span&gt;keyValuePair.Value
          &lt;span style="color: rgb(51, 51, 255);"&gt;select new &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;String&lt;/span&gt;&amp;gt;(keyValuePair.Key, keyValuePair.Value);
SomeMethod(query.ToList());
&lt;/pre&gt;

If nothing else, you don'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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-9067159910767682601?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/9067159910767682601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=9067159910767682601' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/9067159910767682601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/9067159910767682601'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/i-are-tupid.html' title='I are tupid'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-725252919432770938</id><published>2008-07-01T10:03:00.000-07:00</published><updated>2008-07-01T11:27:15.927-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='test'/><title type='text'>Filling a Private Field on a Base Class</title><content type='html'>Ok here's the next step in this testing kick.  So now you have your test classes that create classes.  Swell.  Problem is, there are private fields on the base class of whatever class you are creating.  So you're screwed, right?  Not really.  Now what I am about to do is nothing new.  It's just basically using Reflection and FieldInfo to fill a field on a base class.  Actually very easy.  Here's the code for the example.
&lt;pre&gt;

using System;
using System.Collections.Generic;
using System.Reflection;


&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;UserBase&lt;/span&gt;
{
  &lt;span style="color: rgb(51, 51, 255);"&gt;private &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;_isBaseUser;

  &lt;span style="color: rgb(51, 51, 255);"&gt;public &lt;/span&gt;UserBase()
  {
      _isBaseUser = true;
  }

  &lt;span style="color: rgb(51, 51, 255);"&gt;public &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;IsBaseUser
  {
      get
      {
          &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;_isBaseUser;
      }
  }
}

&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser &lt;/span&gt;: &lt;span style="color: rgb(0, 204, 204);"&gt;UserBase&lt;/span&gt;
{
  &lt;span style="color: rgb(0, 153, 0);"&gt;//Simple list used to "cache" the field info so reflection doesn't have to be used&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;        //again for a type that has already been used.&lt;/span&gt;
  &lt;span style="color: rgb(51, 51, 255);"&gt;private static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt;&amp;gt; typeToInfoList;

  &lt;span style="color: rgb(102, 102, 102);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &lt;span style="color: rgb(0, 153, 0);"&gt;This is used to fill in the needed field on the passed in object.  This is done by reflection/&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &lt;span style="color: rgb(0, 153, 0);"&gt;FieldInfo.  Basically you get the field info you want off the type, then you use the info to&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &lt;span style="color: rgb(0, 153, 0);"&gt;fill the field on the object.&lt;/span&gt;  &lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &amp;lt;param name="objectToFill"&amp;gt;&lt;span style="color: rgb(0, 153, 0);"&gt;This is the object that needs the field changed.&lt;/span&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &amp;lt;param name="fieldName"&amp;gt;&lt;span style="color: rgb(0, 153, 0);"&gt;This is the name of the field.&lt;/span&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &amp;lt;param name="value"&amp;gt;&lt;span style="color: rgb(0, 153, 0);"&gt;This is the value to be set.&lt;/span&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span style="color: rgb(102, 102, 102);"&gt;        /// &amp;lt;param name="typeToCheck"&amp;gt;&lt;span style="color: rgb(0, 153, 0);"&gt;This is the type of the class that the field resides.&lt;/span&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
  &lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;void &lt;/span&gt;FillField(&lt;span style="color: rgb(0, 204, 204);"&gt;Object &lt;/span&gt;objectToFill, &lt;span style="color: rgb(0, 204, 204);"&gt;String &lt;/span&gt;fieldName, &lt;span style="color: rgb(0, 204, 204);"&gt;Object &lt;/span&gt;value, &lt;span style="color: rgb(0, 204, 204);"&gt;Type &lt;/span&gt;typeToCheck)
  {
      &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt; fieldInfoList;
      &lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo &lt;/span&gt;neededFieldInfo;
      &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;heldInfoList;

      &lt;span style="color: rgb(51, 51, 255);"&gt;if &lt;/span&gt;(typeToInfoList == &lt;span style="color: rgb(51, 51, 255);"&gt;null&lt;/span&gt;)
      {
          typeToInfoList = new &lt;span style="color: rgb(0, 204, 204);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt;&amp;gt;();
      }
      &lt;span style="color: rgb(0, 153, 0);"&gt;//Check to see of the list already has the field info and save that &lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;          //boolean for later use.&lt;/span&gt;
      heldInfoList = typeToInfoList.ContainsKey(typeToCheck);
      &lt;span style="color: rgb(0, 153, 0);"&gt;//If it is in the "cache", grab it.  If not, create a new list&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;          //for the passed in type.&lt;/span&gt;
      fieldInfoList = heldInfoList ? typeToInfoList[typeToCheck] : new &lt;span style="color: rgb(0, 204, 204);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;FieldInfo&lt;/span&gt;&amp;gt;(typeToCheck.GetFields(&lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color: rgb(0, 204, 204);"&gt;BindingFlags&lt;/span&gt;.Public));

     &lt;span style="color: rgb(0, 153, 0);"&gt;//Now just look for the needed field info in the list. &lt;/span&gt;
     neededFieldInfo = fieldInfoList.Find(currentItem =&amp;gt; currentItem.Name == fieldName);
      &lt;span style="color: rgb(0, 153, 0);"&gt;//Use the field info to set the value on the object.&lt;/span&gt;
      neededFieldInfo.SetValue(objectToFill, value);

      &lt;span style="color: rgb(0, 153, 0);"&gt;//Store the field info list if it isn't being stored already.&lt;/span&gt;
      if (!heldInfoList)
      {
          typeToInfoList.Add(typeToCheck, fieldInfoList);
      }
  }

  &lt;span style="color: rgb(0, 153, 0);"&gt;//Simple constructor to create the user.&lt;/span&gt;
  &lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser &lt;/span&gt;Create()
  {
      &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser &lt;/span&gt;testUser;

      testUser = new &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser&lt;/span&gt;();

      &lt;span style="color: rgb(51, 51, 255);"&gt;return &lt;/span&gt;testUser;
  }
}
&lt;/pre&gt;
That's pretty much it.  What the hell is all that?  Well basically you have the UserBase as the base class for the example.  MainUser that in inherits UserBase.  The FillField method that does all the work.  And lastly, the dictionary used as a lame cache for this example.  Why cache anything?  Well everything you get the field info, reflection is used.  This can be expensive.  So why bother getting the same field info for the same type every time this method is called?  Just store it somewhere so that if the same class type is passed through again, you can easily access the field info for the class without going for reflection again.
&lt;br /&gt;&lt;br /&gt;
Here's an example of the use:
&lt;pre&gt;
using Microsoft.VisualStudio.TestTools.UnitTesting;

 [&lt;span style="color: rgb(0, 204, 204);"&gt;TestClass&lt;/span&gt;]
 &lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;FillFieldTest&lt;/span&gt;
 {
     [&lt;span style="color: rgb(0, 204, 204);"&gt;TestMethod&lt;/span&gt;]
     &lt;span style="color: rgb(51, 51, 255);"&gt;public void&lt;/span&gt; FillField_CheckFieldForChange()
     {
         &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser &lt;/span&gt;testUser;

         testUser = new &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser&lt;/span&gt;();
         &lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(testUser.IsBaseUser);

         MainUser.FillField(testUser, "_isBaseUser", &lt;span style="color: rgb(51, 51, 255);"&gt;false&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(UserBase));
         &lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsFalse(testUser.IsBaseUser);
      
         &lt;span style="color: rgb(0, 204, 204);"&gt;MainUser&lt;/span&gt;.FillField(testUser, "_isBaseUser", &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;typeof&lt;/span&gt;(UserBase));
         &lt;span style="color: rgb(0, 204, 204);"&gt;Assert&lt;/span&gt;.IsTrue(testUser.IsBaseUser);
     }
 }
&lt;/pre&gt;
First test is checking to see if the IsBaseUser property is true.  This will be true since UserBase sets _isBaseUser to true on instantiation.
&lt;br /&gt;&lt;br /&gt;
Second test is checking to see if the FillField method worked correctly.
&lt;br /&gt;&lt;br /&gt;
Third test is really just to step through a second time so you can see how the quasi-caching works.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-725252919432770938?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/725252919432770938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=725252919432770938' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/725252919432770938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/725252919432770938'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/public-class-userbase-private-boolean.html' title='Filling a Private Field on a Base Class'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-2401015840407364750</id><published>2008-07-01T05:23:00.001-07:00</published><updated>2008-07-01T06:03:30.140-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='Generics'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='test'/><title type='text'>Mulitple Constraint Generics and Test Base Classes</title><content type='html'>This was basically an idea I had to have test classes inherit a TestBase that has a Static Create method on it.  The reason for this is that I have found it easier to have a Create method that takes care of creating a temporary class of the type the test represents.  Say I have a UserTest class and I need an address.  Instead of creating and filling a whole address object in the UserTest, it easier to have a Create Method on the AddressTest class that gives me a premade Address object.  Why would I want to redo a lot of the same code by having the create method on every class when I can have it on a base class and just have the non base classes specify how to create the object?
&lt;br /&gt;&lt;br /&gt;
Is this really needed?  I can't argue absolutely, but this forces a Create method to appear on any test class that inherits from TestBase and by use of Exceptions, forces the child classes to define how to create the object they represent. (As in it forces AddressTest to define how to create an Address object.)  This also allows the Create method to be static and inherited. You could make it abstract, to skip the need for the two create methods(Create and CreateInstance), but that would hose the whole situation since static abstract is a no no.  Static is a must for this situation.
&lt;br /&gt;&lt;br /&gt;
The idea behind the code is to have a static Create method on the TestBase class that  when called will create the test class type passed in through generics.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; I Create()
 {
     I returnValue;
     J testToUse;

     testToUse = System.&lt;span style="color: rgb(0, 204, 204);"&gt;Activator&lt;/span&gt;.CreateInstance&amp;lt;J&amp;gt;();
     returnValue = testToUse.CreateInstance();

     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; returnValue;
 }
&lt;/pre&gt;
Where J is the test class type to create.  Once that test class is instantiated, the virtual method CreateInstance is called.  This is where the test class creates the instance of the object it represents.
&lt;pre&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;protected override&lt;/span&gt; I CreateInstance()
{
     I returnValue;

     returnValue = (I)&lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItem&lt;/span&gt;();
     returnValue.IsSimple = &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;;
     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; returnValue;
}
&lt;/pre&gt;
Where I is the type of object it is going to create. In use it will look something like this:
&lt;pre&gt;
  &lt;span style="color: rgb(0, 204, 204);"&gt;ItemBase&lt;/span&gt; itemToCreate;

  itemToCreate = &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItemTest&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItem&lt;/span&gt;&amp;gt;.Create();
&lt;/pre&gt;
Now for the code:
&lt;pre&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;//Some junk base items to demostrate the creating of an item with a base class&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;ItemBase &lt;/span&gt;
{
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//One item that will be created&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItem &lt;/span&gt;: &lt;span style="color: rgb(0, 204, 204);"&gt;ItemBase&lt;/span&gt;
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;public &lt;/span&gt;&lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;IsSimple { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//The other item that can be created.&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;ComplexItem &lt;/span&gt;: &lt;span style="color: rgb(0, 204, 204);"&gt;ItemBase&lt;/span&gt;
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;IsComplex { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
 &lt;span style="color: rgb(51, 51, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;Boolean &lt;/span&gt;IsNotSimple { &lt;span style="color: rgb(51, 51, 255);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(51, 51, 255);"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//This test will be used to create a SimpleItem&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItemTest&lt;/span&gt;&amp;lt;I&amp;gt;&lt;span style="color: rgb(0, 204, 204);"&gt; &lt;/span&gt;: &lt;span style="color: rgb(0, 204, 204);"&gt;TestBase&lt;/span&gt;&amp;lt;I, &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItemTest&lt;/span&gt;&amp;lt;I&amp;gt;&amp;gt; &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; I : &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItem&lt;/span&gt;
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;protected override&lt;/span&gt; I CreateInstance()
 {
     I returnValue;

     returnValue = (I)&lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;SimpleItem&lt;/span&gt;();
     returnValue.IsSimple = &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;;
     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; returnValue;
 }
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//This test will be used to create a ComplexItem&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;ComplexItemTest&lt;/span&gt;&amp;lt;I&amp;gt; : &lt;span style="color: rgb(0, 204, 204);"&gt;TestBase&lt;/span&gt;&amp;lt;I, &lt;span style="color: rgb(0, 204, 204);"&gt;ComplexItemTest&lt;/span&gt;&amp;lt;I&amp;gt;&amp;gt; &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; I : &lt;span style="color: rgb(0, 204, 204);"&gt;ComplexItem&lt;/span&gt;
{
 &lt;span style="color: rgb(51, 51, 255);"&gt;protected override&lt;/span&gt; I CreateInstance()
 {
     I returnValue;

     returnValue = (I)&lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; ComplexItem();
     returnValue.IsComplex = &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;;
     returnValue.IsNotSimple = &lt;span style="color: rgb(51, 51, 255);"&gt;true&lt;/span&gt;;
     &lt;span style="color: rgb(0, 204, 204);"&gt;return&lt;/span&gt; returnValue;
 }
}

&lt;span style="color: rgb(0, 153, 0);"&gt;//This is the base class where the Create Method resides&lt;/span&gt;
&lt;span style="color: rgb(51, 51, 255);"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(0, 204, 204);"&gt;TestBase&lt;/span&gt;&amp;lt;I, j&amp;gt; &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; I : &lt;span style="color: rgb(51, 51, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;where&lt;/span&gt; J : &lt;span style="color: rgb(0, 204, 204);"&gt;TestBase&lt;/span&gt;&amp;lt;I, j&amp;gt;
{
 &lt;span style="color: rgb(0, 153, 0);"&gt;//This will either be overridden or throw an error.  Kind of a forced&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt; //abstract.  This is the method that the child test classes will use to&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt; //create whatever class they represent.&lt;/span&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;virtual&lt;/span&gt; I CreateInstance()
 {
     &lt;span style="color: rgb(51, 51, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;new&lt;/span&gt; System.&lt;span style="color: rgb(0, 204, 204);"&gt;NotImplementedException&lt;/span&gt;();
 }

 &lt;span style="color: rgb(0, 153, 0);"&gt;//This is used to create a test class and call the CreateInstance method&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt; //so that this method can return the correct object.  The object type is&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt; //dependent on the type of test class that is being created.&lt;/span&gt;
 &lt;span style="color: rgb(51, 51, 255);"&gt;public static&lt;/span&gt; I Create()
 {
     I returnValue;
     J testToUse;

     testToUse = System.&lt;span style="color: rgb(0, 204, 204);"&gt;Activator&lt;/span&gt;.CreateInstance&amp;lt;J&amp;gt;();
     returnValue = testToUse.CreateInstance();

     &lt;span style="color: rgb(51, 51, 255);"&gt;return&lt;/span&gt; returnValue;
 }
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-2401015840407364750?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/2401015840407364750/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=2401015840407364750' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2401015840407364750'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2401015840407364750'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/07/mulitple-constraint-generics-and-test.html' title='Mulitple Constraint Generics and Test Base Classes'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-1765668207916574950</id><published>2008-06-30T05:52:00.000-07:00</published><updated>2008-07-01T05:19:56.578-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='test'/><category scheme='http://www.blogger.com/atom/ns#' term='project'/><title type='text'>Test Projects and Unit Tests Part 1</title><content type='html'>A quick thing about setting up test projects through Visual Studios... basically how to do so.  
&lt;br /&gt;&lt;br /&gt;
Add new Project -&amp;gt; Test (Project Types) -&amp;gt; Test Project.
&lt;br /&gt;&lt;br /&gt;
Now the easiest way I've added actual tests is just to add a normal class.  Say SomeClassTest.
&lt;pre&gt;&lt;b&gt;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class SomeClassTest()
{
   [TestMethod]
   public void SomeClass_CreateWithNull()
   {
   }
}
&lt;/b&gt;&lt;/pre&gt;
What is [TestClass]?  Well that is an attribute you are adding to the class so that .net knows certain methods in it may be test methods. Guess what [TestMethod] does...
&lt;br /&gt;&lt;br /&gt;
Ok so now I have a TestClass and TestMethod, now what?  Well in the menu:
&lt;br /&gt;&lt;br /&gt;
Test -&amp;gt; Windows -&amp;gt; TestView
&lt;br /&gt;&lt;br /&gt;
You should see that your method now shows up on the list.  Simple huh?  If the method isn't showing up on the list make sure it is:
&lt;ul&gt;
&lt;li&gt;
Tagged with the [TestMethod] attribute
&lt;/li&gt;
&lt;li&gt;
Contained in a PUBLIC [TestClass]
&lt;/li&gt;
&lt;li&gt;
Public
&lt;/li&gt;
&lt;li&gt;
void
&lt;/li&gt;
&lt;li&gt;
Non static
&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;&lt;br /&gt;
If you have all of those and still nothing, right click the method name in the Test View and look at it's properties.  There is a property called "Non-Runnable Error".  This will tell you what you need to get it rolling.
&lt;br /&gt;&lt;br /&gt;
All right, now you have everything set... so next?  Simple, right click the method name in the Test View and you can either Run or Debug the test.  If you just Run the test, the Test Results window will pop up and you'll see the progress.  If it passes, then yay.  If not, it will give you things unhandled/expected exception, failed asserts, and expected exceptions.  If you debug, the same happens except, CRAZY SURPRISE AHEAD!!!, you can step through it.  Also, you can run multiple tests by selecting however many you want and running them.
&lt;br /&gt;&lt;br /&gt;
There are some useful options in the test view.  If you right click the column area, you can add columns such as Class Name to help with ordering.  After all, you might have a bunch of methods that are named the same in different classes, which sucks.  Another thing you can do is left click the down arrow to the right of the refresh button for group by options.
&lt;br /&gt;&lt;br /&gt;
&lt;i&gt;End Note: If you don't recognize the word "attribute" that's ok.  Most likely you have seen the [Something] notation before if you have used web controls (Possibly winforms controls, no idea).  Simply put, attributes are like attaching extra information to just about anything so that later on you can use that information at runtime to perform actions.  With the use of reflection, you could, for instance, check for only specific properties on a class that you want to fill dynamically.  Say you have a dataset with the "UserName" column and a User class with the property Name.  If you wanted to fill that Name property normally you could do something like:&lt;/i&gt;
&lt;pre&gt;&lt;b&gt;
user.Name = dataset["UserName"];
&lt;/b&gt;&lt;/pre&gt;
&lt;i&gt;
but with relfection you could get the property info, check the properties for a the ones with a certain attribute, and match the value in the attribute to the column name.
&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-1765668207916574950?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/1765668207916574950/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=1765668207916574950' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1765668207916574950'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/1765668207916574950'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/test-projects-and-unit-tests-part-1.html' title='Test Projects and Unit Tests Part 1'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-8951867534091745594</id><published>2008-06-27T07:53:00.000-07:00</published><updated>2008-07-01T05:20:19.069-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='password'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='hash'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Hashing and you</title><content type='html'>This is really simple, but don't feel bad if you didn't know about this.  No one tells me nothin' either.  
&lt;br /&gt;&lt;br /&gt;
The other day while &lt;a href="http://www.codinghorror.com/blog/archives/001140.html"&gt;reading through a blog, &lt;/a&gt; I got schooled without going to class.  I had no idea that passwords should be one way only, as in you shouldn't be able to retrieve a forgotten password, only reset it.  Well shoot, I missed that one.  So they start talking about hashing the password and saving that.  Good thing I had built something to hash request items in a url to help stop people with screwing with a site url.  Well ok, I didn't totally build it.  I got the idea from somewhere else.  But I f-ing integrated it so don't look at me like that.
&lt;br /&gt;&lt;br /&gt;
Basically you take a password, add a salt (fancy name for a predetermined string or number) to the password, and get the hash value for that.  
&lt;br /&gt;&lt;br /&gt;
&lt;i&gt;Why getting the same hash everytime sucks&lt;/i&gt;
&lt;br /&gt;&lt;br /&gt;
Well the problem with hashing, or really the reason why we are doing this, is that the word "pass" creates the same hash value every time.  Now if some sort of mean person figures out the hash for "pass" (That isn't too hard since most people use some sort of standard like the .net MD5CryptoServiceProvider), he/she could search  for a slew of typical password words.  See the problem?  Now enters the salt.  The salt is some word that you come up with to add anywhere you want.  This makes it a little difficult to figure out since the word "passSalt" is no longer like "pass".  one could try every word known and still fail because said person doesn't know to add the word "Salt".
&lt;br /&gt;&lt;br /&gt;
&lt;i&gt;Why getting the same hash everytime is great:&lt;/i&gt;
&lt;br /&gt;&lt;br /&gt;
Right now you might be wonder what the point of having a password saved that can't be unhashed to check against.  Easy, you don't have to unhash.  Since the hash for "passSalt" will always be the same, the password in the database will always match the entered password + the salt.  So basically the user enters the password, the system adds the salt to the password, the system then gets the hash, and finally checks that against the database record.  Fun huh? 
&lt;br /&gt;&lt;br /&gt;
Now for the code:
&lt;pre&gt;&lt;b&gt;
public class HashString
{
 private const String DEFAULT_SALT = "8745";

 public static String CreateHash(String originalValue, String salt)
 {
   Byte[] computedHash;
   StringBuilder hashedValues;
   StringBuilder hashString;

   &lt;span style="color: rgb(0, 153, 0);"&gt;//Take the string and append your "secret" string at the end.&lt;/span&gt;
   hashString = new StringBuilder(originalValue);
   hashString.Append(salt);
  
   &lt;span style="color: rgb(0, 153, 0);"&gt;//Get the hash for the new word.&lt;/span&gt;
   computedHash = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(hashString.ToString()));
   hashedValues = new StringBuilder();
  
   &lt;span style="color: rgb(0, 153, 0);"&gt;//Go through computedHash and create a string from it.&lt;/span&gt;
   computedHash.ToList().ForEach(currentItem =&gt;
ashedValues.Append(currentItem.ToString("x2")));

   return hashedValues.ToString();
 }

 //Not needed overload, just have it here for ease of use
 public static String CreateHash(String originalValue)
 {
   return CreateHash(originalValue, DEFAULT_SALT);
 }
}
&lt;/b&gt;&lt;/pre&gt;
And I even have a test for you... if you have VS Professional or higher.  If not, no big deal.  Just remove the asserts, attributes, and step through.
&lt;pre&gt;&lt;b&gt;
[TestClass]
public class HashStringTest
{
 private class UserTable
 {
   private const String SALT_VALUE = "SomeValue";
   private List&lt;userrow&gt; table;

   public UserTable()
   {
     table = new List&lt;userrow&gt;();
   }
  
   public void AddUser(String password, String userName)
   {
     table.Add(new UserRow(){Password = HashString.CreateHash(password, SALT_VALUE), UserName = userName});
   }

   public Boolean UserExists(String password, String userName)
   {
     String hashedPassword;

     hashedPassword = HashString.CreateHash(password, SALT_VALUE);

     var query = from user in table
                 where user.Password == hashedPassword &amp;amp;&amp;amp; user.UserName == userName
                 select user;

     return query.ToList().Count == 1;
   }

   private class UserRow
   {
     public String Password { get; set; }
     public String UserName { get; set; }
   }
 }

 [TestMethod]
 public void CreateHash_PasswordMatchPass()
 {
   UserTable table;
   String userName;
   String goodPassword;
   String failedPassword;

   userName = "SomeUser";
   goodPassword = "goodPassword";

   table = new UserTable();
   table.AddUser(goodPassword, userName);
   Assert.IsTrue(table.UserExists(goodPassword, userName));

   failedPassword = "failedPassword";
   Assert.IsFalse(table.UserExists(userName, failedPassword));
 }
}
&lt;/userrow&gt;&lt;/userrow&gt;&lt;/b&gt;&lt;/pre&gt;
And last, the namespaces:
&lt;pre&gt;&lt;b&gt;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
&lt;/b&gt;&lt;/pre&gt;
For the record, I hate the word salt in this use.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-8951867534091745594?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/8951867534091745594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=8951867534091745594' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8951867534091745594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/8951867534091745594'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/hashing-and-you.html' title='Hashing and you'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-3509336644801602376</id><published>2008-06-26T09:30:00.000-07:00</published><updated>2008-07-01T05:21:51.437-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Random'/><category scheme='http://www.blogger.com/atom/ns#' term='String'/><title type='text'>Random String of Specified Length with Enumerable</title><content type='html'>&lt;pre&gt;&lt;b&gt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public static String RandomString(Int32 length)
{
 Random randomGenerator;
 String returnValue;

  randomGenerator = new Random();

 returnValue = new string(Enumerable.Range(0, length).Select(i =&amp;gt; (char)('A' + randomGenerator.Next(0, 25))).ToArray());

 return returnValue;
}
&lt;/b&gt;&lt;/pre&gt;
What this does:

Enumerable.Range basically says, "Give me a collection of numbers from the first parameter to the second", or in this case from 0 to length.

Select basically is a method that takes in an anonymous method (Lamdba expression in this case), goes through each of the items in the collection, and runs the anonymous method for every item in the collection.  Not exactly sure what Select does specifically, but it is most likely something like this: (roughish psuedo code)
&lt;pre&gt;&lt;b&gt;
public static Array Select(Func func))
{

 Array returnValue;

 foreach(Int32 currentItem in Items)
 {
    returnValue.Add(func());
 }

 return returnValue;

}
&lt;/b&gt;&lt;/pre&gt;
Where Func is:
&lt;pre&gt;&lt;b&gt;
public Char Func()
{
 return  (char)('A' + randomGenerator.Next(0, 25);
}
&lt;/b&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-3509336644801602376?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/3509336644801602376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=3509336644801602376' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/3509336644801602376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/3509336644801602376'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/using-system-using-system.html' title='Random String of Specified Length with Enumerable'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-9096276713326488006</id><published>2008-06-26T06:18:00.000-07:00</published><updated>2008-06-26T06:49:32.104-07:00</updated><title type='text'>Using the ForEach method on List Collections</title><content type='html'>&lt;pre&gt;&lt;b&gt;
public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

&lt;span style="color: rgb(0, 153, 0);"&gt;  //baseList is some list I wasn't nice enough to show where it came from.&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;  //It's just a list of states.  Get over it.&lt;/span&gt;
 var stateList = from state in baseList
                 where state.Name.StartsWith(partialName)
                 select new State(state.Name, state.Code);

 stateList.ToList().ForEach(currentState =&amp;gt; returnValue.Add(currentState));

 return returnValue;
}
&lt;/b&gt;&lt;/pre&gt;

So what is done here:

Basically I am using a linq expression to get states from a list of states (Like Michigan, Illinois, or Canada) based on name.  No big deal.  Then I take the query and produce a List from it.  AMAZIN!
&lt;pre&gt;&lt;b&gt;
stateList.ToList().&lt;span style="color: rgb(51, 153, 153);"&gt;ForEach(curretState =&amp;gt; returnValue.Add(curretState));&lt;/span&gt;
&lt;/b&gt;&lt;/pre&gt;
ForEach is an extension method for List&amp;lt;&amp;gt;.  Now if you remember from previous posts, that means it resides in a static class somewhere that "tacks" it on to the List&amp;lt;&amp;gt; class.  Basically this method acts like a typical foreach loop.  (You know, go through the list and do something. Or nothing.  I suppose you could just loop through if you wanted to.  I won't tell you how to live your life.)  Simple but in my opinion, much cleaner looking.

I mean I could do this:
&lt;pre&gt;&lt;b&gt;
public static StateCollection GetByName(String partialName)
{
 StateCollection returnValue;

 returnValue = new StateCollection();

 foreach(State currentState in baseList)
 {
   if(currentState.Name.StartsWith(partialName)
   {
     returnValue.Add(currentState);
   }
 }

 return returnValue;
}
&lt;/b&gt;&lt;/pre&gt;
Really it's just up to what you prefer.  (And I'm sure you could drive a car with no power brakes.  You'll still get there.)  Also, I really didn't need the linq expression since I could have done this all with ForEach (Provided baseList is IEnumerable).

One last note, all IEnumerable collections have the ToList() method (And a bunch of other ones for that matter.)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-9096276713326488006?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/9096276713326488006/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=9096276713326488006' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/9096276713326488006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/9096276713326488006'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/using-foreach-method-on-list.html' title='Using the ForEach method on List Collections'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-7835917173587175679</id><published>2008-06-25T14:04:00.000-07:00</published><updated>2008-07-01T05:21:05.977-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='Enumeration'/><category scheme='http://www.blogger.com/atom/ns#' term='Random'/><title type='text'>Random Enumeration Generator with Generics</title><content type='html'>&lt;pre&gt;&lt;b&gt;
public static I RandomEnumeration&amp;lt;I&amp;gt;()
{
  I enumerationToCheck;
  Int32 indexToUse;
  String[] names;

  &lt;span style="color: rgb(0, 153, 0);"&gt;//Use activator to create an instance of the type I&lt;/span&gt;
  enumerationToCheck = System.Activator.CreateInstance&amp;lt;I&amp;gt;();

  &lt;span style="color: rgb(0, 153, 0);"&gt;//Make sure the instance is an Enumeration&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;   //Unfortunately you can't check that in the method &lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;   //delcaring using "which".&lt;/span&gt;
  if (enumerationToCheck as Enum == null)
  {
    throw new InvalidOperationException();
  }

  &lt;span style="color: rgb(0, 153, 0);"&gt;//Get the list of the enumeration item names&lt;/span&gt;
  names = Enum.GetNames(typeof(I));

  if (names.Length &amp;gt; 0)
  {
    &lt;span style="color: rgb(0, 153, 0);"&gt;//Grab a random name within the boundaries of the&lt;/span&gt;
&lt;span style="color: rgb(0, 153, 0);"&gt;     //names collection.&lt;/span&gt;
    indexToUse = RandomInt32(0, names.Length);
    &lt;span style="color: rgb(0, 153, 0);"&gt;//parse the name to create the random enum&lt;/span&gt;
    enumerationToCheck = (I)Enum.Parse(typeof(I), names[indexToUse]);
  }

  return enumerationToCheck;

}
&lt;/b&gt;&lt;/pre&gt; 
Usage:
&lt;pre&gt;&lt;b&gt;
  SomeEnum test = RandomEnumeration();
&lt;/b&gt;&lt;/pre&gt;
Why bother?  For unit testing and creating test classes.  Possibly
for defaults on an enumeration, but not really needed since
they are value types.  Oh yeah AND BECAUSE I FELT LIKE IT. I don't
have to explain myself to you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-7835917173587175679?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/7835917173587175679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=7835917173587175679' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7835917173587175679'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7835917173587175679'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/public-static-i-randomenumeration-i.html' title='Random Enumeration Generator with Generics'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-2624911148462616178</id><published>2008-06-25T13:42:00.000-07:00</published><updated>2008-06-25T14:01:48.417-07:00</updated><title type='text'>IEnumerable Extention Methods and Action</title><content type='html'>This may be slightly off, but I've pretty much figured them out and how they work with lambda expressions.

First off, Lambda expressions.  These are the odd looking currentItem =&amp;gt; expressions you might see in my examples.  They are a little misleading, at least to me they were.  When I saw:
&lt;pre&gt;&lt;b&gt;
 ilist.SomeExtension(currentItem =&amp;gt; SomeMethod(currentItem));
&lt;/b&gt;&lt;/pre&gt;
I thought that meant:

Loop through the list of currentItems and use that method.  Well that's sort of it, but really misleading.  In order to fully understand some of the more fun extension methods and lamba fun, a clearer explanation is needed. (I say fun instead of more complicated since I think the word "complicated" is exaggerating .)

What 
&lt;pre&gt;&lt;b&gt;
 ilist.SomeExtension(currentItem =&amp;gt; SomeMethod(currentItem));
&lt;/b&gt;&lt;/pre&gt;
really means is that I am going to call this method SomeExtension, give it a method to use, and that's it. What it does with that method is the heart of what SomeExtension is.  

&lt;i&gt;Small note:  currentItem =&amp;gt; helps to infer the type of whatever it is that you're going to pass into SomeMethod.&lt;/i&gt; 

Say SomeExtension looks like this:
&lt;pre&gt;&lt;b&gt;
public static void SomeExtension(this IList&amp;lt;I&amp;gt; items, &amp;lt;I&amp;gt;, Action&amp;lt;I&amp;gt; action)
{
   SomeExtension(I item in items)
  {
    action(item);
  }
}
&lt;/b&gt;&lt;/pre&gt;
Now what is action?  It's basically a place holder for a method that returns nothing.  Only thing it cares about is what it has to send in, and that is Type I.  From there, it will call the method it points to and magic happens.
&lt;i&gt;Small note:  What the hell is items?  Well that is the list that you called this method from.  This is an example of an extension method.  Extension methods, in short, allow a person to "tack" a method onto a class without changing the class itself.  &lt;b&gt;this IList&amp;lt;I&amp;gt; items&lt;/b&gt; tells me that the extension method SomeExtension will be "tacked" onto ANY IList. &lt;/i&gt; 

In this instance, what action really looks like is this:
&lt;pre&gt;&lt;b&gt;
public void action
(
  delegate (I Item)
  {
    SomeMethod(item);
  }
)
&lt;/b&gt;&lt;/pre&gt;
As you can see, the SomeMethod from the original SomeExtension call comes into play now.  The above examples really could be the List&amp;lt;&amp;gt; extention method of ForEach most likely looks like.

Now what might not look familiar there is the delegate key word.  This is what's called an anonymous method.  Basically, this allows you to create delegates on the fly.  An example of this would be List&amp;lt;&amp;gt;.Contains.  A normal call to this might be:
&lt;pre&gt;&lt;b&gt;
someList.Contains(delegate(String currentItem){ return currentItem == someVariable); });
&lt;/b&gt;&lt;/pre&gt;
What this says is that contains will do whatever it does, but it will use the method you have provided to do that.  Most likely the method itself looks like (And this is pseudo code for sure):
&lt;pre&gt;&lt;b&gt;
Boolean contains;

contains = false;

foreach(String currentItem in theList)
{
  contains = delegate(currentItem);

  if(contains)
  {
     exit;
  }
}

return contains;
&lt;/b&gt;&lt;/pre&gt;
Although, this is misleading in this post since this would deal with Func not Action.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-2624911148462616178?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/2624911148462616178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=2624911148462616178' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2624911148462616178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/2624911148462616178'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/this-may-be-slightly-off-but-ive-pretty.html' title='IEnumerable Extention Methods and Action'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-7221829967093665316</id><published>2008-06-25T09:43:00.000-07:00</published><updated>2008-06-25T13:22:52.547-07:00</updated><title type='text'>Filling a list of classes with random values using linq</title><content type='html'>Filling a list of classes (10 for this example) with random values using linq.

&lt;i style="font-weight: bold; color: rgb(51, 153, 153);"&gt;Enumerable.Range(0, 10)&lt;/i&gt;&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt; &lt;/span&gt;
- This means give me a list from 1 to 10

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10)&lt;span style="color: rgb(102, 204, 204);"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;i style="font-weight: bold; color: rgb(51, 153, 153);"&gt;Select
(
  j&lt;/i&gt;&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt; =&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
-  This means you are going to select every j in the list Enumerable created

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;(&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;  j =&amp;gt; new JunkItemB()&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;
- For every J in the list, create a new junkItem

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;(&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  j =&amp;gt; &lt;/span&gt;&lt;i style="font-weight: bold;"&gt;new JunkItemB()
&lt;span style="color: rgb(51, 153, 153);"&gt;  {&lt;/span&gt;
&lt;span style="color: rgb(51, 153, 153);"&gt;    SomeNumber&lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt; = Enumerable.Range(0, 1).Select&lt;/span&gt;
&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt;    (&lt;/span&gt;
&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt;      i =&amp;gt; randomGenerator.Next()&lt;/span&gt;
&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt;    ).ElementAt(0)&lt;/span&gt;
&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt;  }&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;
&lt;/pre&gt;
- This is the new way to set a property in 3.5. I could just use a constructor, and probably should but this was just thrown together.

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;(&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  j =&amp;gt; new JunkItemB()&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  {&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    SomeNumber = &lt;/span&gt;&lt;i style="font-weight: bold;"&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;Enumerable.Range(0, 1).Select&lt;/span&gt;
    (
      i =&amp;gt; randomGenerator.Next()
    )&lt;/i&gt;&lt;span style="font-weight: bold;"&gt;.ElementAt(0)&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  }&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;)&lt;/span&gt;
&lt;/pre&gt;
- Once again, I am using Enumerable to create a list.  This one will have 1 item. I will use Select to go through the list.For every i in the list, test (this is the Random object) will create a random number and give me a list of random numbers the size of the Enumerable.Range list.

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;(&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  j =&amp;gt; new JunkItemB()&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  {&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    SomeNumber = Enumerable.Range(0, 1).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    (&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;      i =&amp;gt; randomGenerator.Next()&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    )&lt;span style="color: rgb(51, 153, 153);"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;i style="font-weight: bold; color: rgb(51, 153, 153);"&gt;ElementAt(0)&lt;/i&gt;
&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;).ToList()&lt;/span&gt;
&lt;/pre&gt;
- I just want one random number, so I will get the number t the first index. Although with this, I only have on number in the list anyhow. The property of the current JunkItemB will be set to this.  Once this is done, it will be repeated 9 more times.

&lt;pre&gt;
&lt;span style="font-weight: bold;"&gt;Enumerable.Range(0, 10).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;(&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  j =&amp;gt; new JunkItemB()&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;  {&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    SomeNumber = Enumerable.Range(0, 1).Select&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    (&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;      i =&amp;gt; randomGenerator.Next()&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;    ).ElementAt(0)&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt; }&lt;/span&gt;
&lt;span style="font-weight: bold;"&gt;).&lt;/span&gt;&lt;i&gt;&lt;span style="font-weight: bold; color: rgb(51, 153, 153);"&gt;ToList()&lt;/span&gt;
&lt;/pre&gt;
&lt;/i&gt;- Now that I have my list of 10 JunkItemBs, I will create a new list and from it giving me 10 JunkItemBs with preset SomeNumber properties.


Now that I look at this again (This is taken from notes I had somewhere else) I could have just used Random to get a number instead of Enumerator. Yay for over complicating things.  Also my explanation of Select is a bit misleading for those who haven't used lambda expressions.  I'll explain further in my next post.

Namespaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-7221829967093665316?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/7221829967093665316/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=7221829967093665316' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7221829967093665316'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/7221829967093665316'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/filling-list-of-classes-with-random.html' title='Filling a list of classes with random values using linq'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294677086277828431.post-402540345038532627</id><published>2008-06-25T08:02:00.000-07:00</published><updated>2008-06-25T09:22:18.404-07:00</updated><title type='text'>All good things come to an end...</title><content type='html'>but they never say much about bad things and beginnings.  I guess we'll just have to see where this ends up.  I figure if this thing never ends, then it's pretty obvious what this is.

It's also going to be obvious as to what this is not.  This is not a place for incredible solutions to programming problems.  This is not written by an expert.  I don't work for some kind of think tank.  I haven't been programming since I was 2.  Simply put, I am a moderate programmer that for some reason has decided to take programming seriously and figure out all I can about .net 3.5 and beyond... without having much time with 2.0.  In other words, FUN.

I can't swear that everything I put in here is 100% accurate.  It should build, but my explanations could be a bit off.  Well let me put that in another way... they are off in that they most likely will not make sense the first time around.  They also could be somewhat wrong.  Either way, for those 2 people reading this, please leave a comment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294677086277828431-402540345038532627?l=byatool.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://byatool.blogspot.com/feeds/402540345038532627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294677086277828431&amp;postID=402540345038532627' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/402540345038532627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294677086277828431/posts/default/402540345038532627'/><link rel='alternate' type='text/html' href='http://byatool.blogspot.com/2008/06/all-good-things-come-to-end.html' title='All good things come to an end...'/><author><name>Sean</name><uri>http://www.blogger.com/profile/16514095708971143799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://bp2.blogger.com/_4SgzvK41a_8/SGJ0mqrQSiI/AAAAAAAAAAU/0wFyUWwMPfA/S220/RenaissanceCenter.jpg'/></author><thr:total>0</thr:total></entry></feed>
