<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FailBoy &#187; C#</title>
	<atom:link href="http://www.failboy.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.failboy.net</link>
	<description>We reserve the right FAIL</description>
	<lastBuildDate>Thu, 26 Nov 2009 06:29:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C# has an ISNULL function, its called ??</title>
		<link>http://www.failboy.net/2009/10/c-has-an-isnull-function-its-called/</link>
		<comments>http://www.failboy.net/2009/10/c-has-an-isnull-function-its-called/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 08:13:27 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[??]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ISNULL]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=48</guid>
		<description><![CDATA[Just a very short and quick post. For a while now .Net has had nullable types. Take for example an integer, instead of declaring it as: int x = 0; you declare it as: int? x = 0; Now in coding examples all over the web I&#8217;m seeing the same logic and its driving me [...]]]></description>
			<content:encoded><![CDATA[<p>Just a very short and quick post. For a while now .Net has had nullable types. Take for example an integer, instead of declaring it as:</p>
<pre class="brush: csharp;">int x = 0;</pre>
<p>you declare it as: </p>
<pre class="brush: csharp;">int? x = 0;</pre>
<p>Now in coding examples all over the web I&#8217;m seeing the same logic and its driving me nuts!</p>
<pre class="brush: csharp;">if(x == null){
    x = -1;
}</pre>
<p>There is a much shorter route and that is:</p>
<pre class="brush: csharp;">x = x ?? -1;</pre>
<p>The &#8220;??&#8221; operator works exactly the same way an ISNULL function call works in SQL. It evaluates the variable on the left of the operator to ensure its not null (or undefined as some people call it), and if it is, it uses the value on the right of the ?? operator. Quick and easy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/10/c-has-an-isnull-function-its-called/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use Lambda Expressions</title>
		<link>http://www.failboy.net/2009/07/how-to-use-lambda-expressions/</link>
		<comments>http://www.failboy.net/2009/07/how-to-use-lambda-expressions/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 04:46:32 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Lambda Expressions]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=30</guid>
		<description><![CDATA[Until I recently started working at Chase Software I hadn&#8217;t really dived to deep into the C# 3.0 features. I really wish I had though because man it would&#8217;ve cut down so much code at my previous job. A colleague of mine has taught me so much that I am now looking a lot deeper [...]]]></description>
			<content:encoded><![CDATA[<p>Until I recently started working at <a href="http://www.chasesoftware.co.za" target="_blank">Chase Software</a> I hadn&#8217;t really dived to deep into the C# 3.0 features. I really wish I had though because man it would&#8217;ve cut down so much code at my previous job. A <a href="http://www.twitter.com/jabezzz" target="_blank">colleague of mine</a> has taught me so much that I am now looking a lot deeper into C# 3 features. Just in time before C# 4.0 is released! One of the first things I learned to get the hang of is Lambda expressions. Lambdas are inline expressions. These expressions are mainly used in conjunction with LiNQ and thus mostly on IQueryable and IEnumerable collections. Here is an example on how it can cut a lot of code out of you work.</p>
<p>Assume we have  a basic class that contains a Name, ID and AwesomeScore for Users.</p>
<pre class="brush: csharp;">public class Users
{
public int ID {get; set;}
public string Name {get; set;}
public int AwesomeScore {get; set;}
}</pre>
<p>So as you can see there&#8217;s nothing difficult about that. Its a basic class definition. So lets create a basic console application and fill a List of Users so we can work with that:</p>
<pre class="brush: csharp;">public static void Main(string[] args)
{
// Create and populate new list of Users
List&lt;Users&gt; failBoyUsers = new List&lt;Users&gt;();
failBoyUsers.Add(new Users { AwesomeScore = 7, Name = &quot;FailBoy&quot;, ID = 1 });
failBoyUsers.Add(new Users { AwesomeScore = 10, Name = &quot;Rathlan&quot;, ID = 2 });
failBoyUsers.Add(new Users { AwesomeScore = 9, Name = &quot;Joe&quot;, ID = 3 });
failBoyUsers.Add(new Users { AwesomeScore = 1, Name = &quot;newbie&quot;, ID = 4 });
}</pre>
<p>So here we create a Generic List Collection of Users. So our failBoyUsers collection now contains 4 users with varying scores. Now, originally before the wonders of learning how to use Lambda&#8217;s, I would&#8217;ve done the following to return the users with an AwesomeScore of 8 or over:</p>
<pre class="brush: csharp;">
public static List GetAwesomestPeople(List&lt;Users&gt; usersToSort)
{
List&lt;Users&gt; returnList = new List&lt;Users&gt;();

foreach (Users userItem in usersToSort)
{
if (userItem.AwesomeScore &gt;= 8 )
{
returnList.Add(userItem);
}
}

return returnList;
}</pre>
<p>So yeah, that was me, FailBoy being very intelligent. Now, that same functionality using Lambda&#8217;s.</p>
<pre class="brush: csharp;">
public static IQueryable GetAwesomestPeopleLAMBDA(IQueryable usersToSort)
{
return usersToSort.Where(user =&gt; user.AwesomeScore &gt;= 8).AsQueryable();
}
</pre>
<p>Thats right! Using Lambda&#8217;s its a single line. So lets look at that line, I pass in the usersToSort as IQueryable instead of the List. Converting from the List to IQueryable is as easy as passing the List parameter in and simply adding .AsQueryable() to the end of the parameter name! So it would be called as :</p>
<pre class="brush: csharp;">IQueryable awesomeOkes = GetAwesomestPeopleLAMBDA(failBoyUsers.AsQueryable());</pre>
<p>Then I use the .Where() method which is an extension method on the IQueryable collection class and as such on the List&lt;&gt; class since List&lt;&gt; inherits from IEnumerable and IEnumerable inherits from IQueryable. The fun comes in where I pass the argument into the .Where() method.</p>
<pre class="brush: csharp;">user =&gt; user.AwesomeScore &gt;= 8</pre>
<p>Here I am declaring a new variable called user. The variable is an instance of the Collection we want to sort, in this case usersToSort. the &#8220;=&gt;&#8221; is what is referred to as the Lambda operator. After the Lambda operator we specify our condition. In this as we only want the users with an AwesomeScore of 8 or higher. I then append the</p>
<pre class="brush: csharp;">.AsQueryable()</pre>
<p>because we want to return the resultset as an IQueryable Collection of Users.</p>
<p>Using Lambda&#8217;s I&#8217;m condensed 9 lines of code into 1 line of code. And the less you need to code, the more time you save! I hope this is helpful to you! I&#8217;ve included the full code below.</p>
<p>FailBoy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/07/how-to-use-lambda-expressions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SqlParameter is already contained by another SqlParameterCollection</title>
		<link>http://www.failboy.net/2009/05/sqlparameter-is-already-contained-by-another-sqlparametercollection/</link>
		<comments>http://www.failboy.net/2009/05/sqlparameter-is-already-contained-by-another-sqlparametercollection/#comments</comments>
		<pubDate>Wed, 13 May 2009 17:00:50 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SqlParameter]]></category>
		<category><![CDATA[SqlParameterCollection]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=15</guid>
		<description><![CDATA[I discovered an interesting bug today. While executing some SQL Stored Procedures via C# I was trying to reuse a SqlParameter. However, when the SqlParameter was added to a second SqlCommand, it would throw an error when trying to execute the second command. The error encountered was the &#8220;SqlParameter is already contained by another SqlParameterCollection&#8220;. [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered an interesting bug today. While executing some SQL Stored Procedures via C# I was trying to reuse a SqlParameter. However, when the SqlParameter was added to a second SqlCommand, it would throw an error when trying to execute the second command. The error encountered was the &#8220;<em><strong>SqlParameter is already contained by another SqlParameterCollection</strong></em>&#8220;.</p>
<p>It seems that .Net&#8217;s garbage collection retains a reference to the SqlParameter used in the first SqlCommand. This reference seems to survive the closing and even disposing of the connection in use. As long as the SqlCommand object is in use, .Net seems to remember that it has already been used. To bypass this, simply call the <em><strong>sqlCommand.Parameters.Clear()</strong> </em>method. You&#8217;ll need to readd the SqlParameter or SqlParameter[] to the SqlCommand and it&#8217;ll work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/05/sqlparameter-is-already-contained-by-another-sqlparametercollection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
