<?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; .Net Framework</title>
	<atom:link href="http://www.failboy.net/category/dev/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.failboy.net</link>
	<description>We reserve the right FAIL</description>
	<lastBuildDate>Tue, 03 Aug 2010 20:45:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>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>LINQ: Distinct method</title>
		<link>http://www.failboy.net/2009/07/linq-distinct-method/</link>
		<comments>http://www.failboy.net/2009/07/linq-distinct-method/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 19:44:14 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Distinct]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=42</guid>
		<description><![CDATA[Ah the beauty of LINQ. I previously did a post on How to use Lambda Expressions and if you aren&#8217;t sure now how to use them, is the time to check it out. I have decided to have a good look at the Methods available to IEnumerable collections and first on the list the DISTINCT [...]]]></description>
			<content:encoded><![CDATA[<p>Ah the beauty of LINQ. I previously did a post on<a title="how to use lambda expressions" href="http://www.failboy.net/2009/07/how-to-use-lambda-expressions/" target="_blank"> How to use Lambda Expressions</a> and if you aren&#8217;t sure now how to use them, is the time to check it out. I have decided to have a good look at the Methods available to IEnumerable collections and first on the list the DISTINCT method.</p>
<p><span style="text-decoration: underline;"><strong>The Boring Standard Method:</strong></span></p>
<p>The Distinct method removes any duplicate entries in a collection without you needing to do much work, the same as the DISTINCT keyword inT-SQL. Here is the basic and standard use that is really not difficult to understand.</p>
<pre class="brush: csharp;">
string[] names = new string[] { &quot;Peter&quot;, &quot;Paul&quot;, &quot;Mary&quot;, &quot;Peter&quot;, &quot;Paul&quot;, &quot;Mary&quot;, &quot;Janet&quot; };

foreach (string _name in names.Distinct())
{
Console.WriteLine(_name);
}</pre>
<p>So like I said, easy enough, we have a string array with names and by using the .Distinct method we remove any duplicates from the resultset that is returned. Now comes the real fun!</p>
<p><span style="text-decoration: underline;"><strong>Custom Distinct Implementation:</strong></span></p>
<p>Since the advent of generics, most of us have been creating strongly typed lists of data. Plain example would be a list of our customer UserClass.</p>
<pre class="brush: csharp;">
public class Users
{
public int ID {get; set;}
public string Name {get; set;}
public int AwesomeScore {get; set;}
}

// 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 = 10, Name = &quot;Rathlan&quot;, ID = 2 });
failBoyUsers.Add(new Users { AwesomeScore = 7, Name = &quot;Joe&quot;, ID = 3 });
failBoyUsers.Add(new Users { AwesomeScore = 7, Name = &quot;Joe&quot;, ID = 3 });
failBoyUsers.Add(new Users { AwesomeScore = 7, Name = &quot;Joe&quot;, ID = 3 });
failBoyUsers.Add(new Users { AwesomeScore = 1, Name = &quot;newbie&quot;, ID = 4 });
</pre>
<p>This would then give us a &#8220;strongly typed Array&#8221; of our User class. If we now had to call the .Distinct function on the collection it wouldn&#8217;t work! Thats because the CLR does not know what to evaluate to decide whether an entry is unique or a duplicate so it returns all the items including the duplicates. The Distinct method accepts an overload paramter, this object must implement the IEqualityComparer interface. So we declare a new class and implement IEqualityComparer as follows:</p>
<pre class="brush: csharp;">
public class AwesomeComparer : IEqualityComparer&lt;Users&gt;
{
public bool Equals(Users x, Users y)
{
if (x == null || y == null)
return false;
else
return (x.Name.ToLower() == y.Name.ToLower() &amp;&amp;
x.AwesomeScore == y.AwesomeScore &amp;&amp;
x.ID == y.ID);
}

public int GetHashCode(Users obj)
{
return obj.Name.GetHashCode();
}
}
</pre>
<p>so what we do in the Equals Method is define what we feel a duplicate of our custom class looks like. We also implement the GetHashCode method to return the hash of the &#8220;primary field&#8221;. This will usually be the Primary Key that you class will be based on. Then we implement our Distinct method, we do so as follows:</p>
<pre class="brush: csharp;">
AwesomeComparer awesomeComparer = new AwesomeComparer();

foreach (Users usr in failBoyUsers.Distinct(awesomeComparer))
{
Console.WriteLine(usr.ID.ToString() + &quot; - &quot; + usr.Name + &quot; - &quot; + usr.AwesomeScore.ToString());
}
</pre>
<p>And that will only return Distinct values from our custom class! I hope this helps someone at some stage as I never knew about this myself and stumbled onto it. Drop me a comment if you think I suck or if my some margain of luck I managed to produce some bug free code, I am after FailBoy for a reason <img src='http://www.failboy.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/07/linq-distinct-method/feed/</wfw:commentRss>
		<slash:comments>2</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>Set Forms Authentication Path dynamically</title>
		<link>http://www.failboy.net/2009/05/set-forms-authentication-path/</link>
		<comments>http://www.failboy.net/2009/05/set-forms-authentication-path/#comments</comments>
		<pubDate>Tue, 26 May 2009 17:00:41 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[path]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=26</guid>
		<description><![CDATA[Lets say we have IIS with a default website and two Virtual Directories, both containing an instance of an application. So when a user logs into Instance A and then into Instance B, he is then logged out of Instance A. In testing I saw that if I hardcoded the path attribute on the forms [...]]]></description>
			<content:encoded><![CDATA[<p>Lets say we have IIS with a default website and two Virtual Directories, both containing an instance of an application. So when a user logs into Instance A and then into Instance B, he is then logged out of Instance A. In testing I saw that if I hardcoded the path attribute on the forms tag under authentication in the web.config then the systems would work fine and not effect each other. So I set about trying to set the path dynamically but I was doing it all over and it wasn’t working.</p>
<p>My stupidity lead me into trying to set the Path on each cookie. That, however,  wasn’t the problem. A Colleague of mine found the solution this morning though. After we log the user into the system we fire the following code:</p>
<blockquote><p><code>FormsAuthentication.RedirectFromLoginPage(authToken, true);</code></p></blockquote>
<p>However the RedirectFromLoginPage does take a 3rd parameter which will be the path that I’ve been trying to set. So now the code reads:</p>
<blockquote><p><code>string applicationBasePath = Request.ApplicationPath;<br />
FormsAuthentication.RedirectFromLoginPage(authToken, chkRememberMe.Checked, applicationBasePath);</code></p></blockquote>
<p>So once that was done everything worked like a charm!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/05/set-forms-authentication-path/feed/</wfw:commentRss>
		<slash:comments>0</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>
		<item>
		<title>Improve your server Garbage Collection Performance</title>
		<link>http://www.failboy.net/2009/05/improve-your-server-garbage-collection-performance/</link>
		<comments>http://www.failboy.net/2009/05/improve-your-server-garbage-collection-performance/#comments</comments>
		<pubDate>Wed, 06 May 2009 12:24:45 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Garbage Collection]]></category>
		<category><![CDATA[Servers]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=9</guid>
		<description><![CDATA[I came across a little known config of ASP.Net applications. We all know about the built-in Garbage Collection system that the .Net Framework uses to recycle &#8220;dead&#8221; or unused objects from memory and recycle the space. By default the Garbage Collector is set to &#8220;Workstation&#8221; Mode. That means the Garbage collector will be efficient on [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a little known config of ASP.Net applications. We all know about the built-in Garbage Collection system that the .Net Framework uses to recycle &#8220;dead&#8221; or unused objects from memory and recycle the space. By default the Garbage Collector is set to &#8220;Workstation&#8221; Mode. That means the Garbage collector will be efficient on a machine with a single processor and little RAM. This is usually the case on most developer PCs. However, when we deploy our applications to the server we usually have hugely powerful servers that have multiple cores and gigs of RAM. On these servers we should enable the Garbage Collection to run on Server Mode. The application will be more CPU and RAM intensive but the garbage collection will be a lot more responsive and active in monitering the objects currently in memory. To do this, in your web.config you need to add the following section:</p>
<blockquote>
<pre id="ctl00_rs1_mainContentContainer_ctl20other" class="libCScode">&lt;configuration&gt;
   &lt;runtime&gt;
      &lt;gcServer enabled="true"/&gt;
   &lt;/runtime&gt;
&lt;/configuration&gt;</pre>
</blockquote>
<p>Why should you enable this though? How does it benefit your application? It comes down to the performance of your application on the server. Not how quick your page loads, but how well your application handles large volumes of traffic without your application falling over and dying. Here&#8217;s why! Server Garbage Collection creates one GC heap per processor, which are collected in parallel. This GC mode maximizes the number of requests per second. So imagine having a server with 4 gigs of RAM and a quad core CPU, your application would be able to withstand much larger amounts of traffic without the application running over of memory! Effectively, on a quad core CPU, you would have 4 Garbage Collectors working in unison!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/05/improve-your-server-garbage-collection-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: TinyUrl conversion code</title>
		<link>http://www.failboy.net/2009/05/c-tinyurl-conversion-code/</link>
		<comments>http://www.failboy.net/2009/05/c-tinyurl-conversion-code/#comments</comments>
		<pubDate>Wed, 06 May 2009 12:23:49 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[TinyUrl]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=7</guid>
		<description><![CDATA[I am currently working on a small project using ASP.Net MVC and I want to incorporate a TinyUrl facility such as that of www.tinyurl.com and the million other clones there are. I found this awesome piece of code that does the trick beautifully! I found the code @ http://blogs.msdn.com/bramveen/archive/2009/01/06/converting-url-to-tinyurl-in-c.aspx It was really quick to implement [...]]]></description>
			<content:encoded><![CDATA[<p>I am currently working on a small project using ASP.Net MVC and I want to incorporate a TinyUrl facility such as that of www.tinyurl.com and the million other clones there are. I found this awesome piece of code that does the trick beautifully! I found the code @ <a title="TinyUrl code" href="http://blogs.msdn.com/bramveen/archive/2009/01/06/converting-url-to-tinyurl-in-c.aspx" target="_blank">http://blogs.msdn.com/bramveen/archive/2009/01/06/converting-url-to-tinyurl-in-c.aspx</a></p>
<p>It was really quick to implement and works really nicely.</p>
<pre class="code"><span style="color: blue;">protected string </span>ToTinyURLS(<span style="color: blue;">string </span>txt)
{
    <span style="color: #2b91af;">Regex </span>regx = <span style="color: blue;">new </span><span style="color: #2b91af;">Regex</span>(<span style="color: #a31515;">"http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"</span>, <span style="color: #2b91af;">RegexOptions</span>.IgnoreCase);

    <span style="color: #2b91af;">MatchCollection </span>mactches = regx.Matches(txt);

    <span style="color: blue;">foreach </span>(<span style="color: #2b91af;">Match </span>match <span style="color: blue;">in </span>mactches)
    {
        <span style="color: blue;">string </span>tURL = MakeTinyUrl(match.Value);
        txt = txt.Replace(match.Value, tURL);
    }

    <span style="color: blue;">return </span>txt;
}

<span style="color: blue;">public static string </span>MakeTinyUrl(<span style="color: blue;">string </span>Url)
{
    <span style="color: blue;">try
    </span>{
        <span style="color: blue;">if </span>(Url.Length &lt;= 12)
        {
            <span style="color: blue;">return </span>Url;
        }
        <span style="color: blue;">if </span>(!Url.ToLower().StartsWith(<span style="color: #a31515;">"http"</span>) &amp;&amp; !Url.ToLower().StartsWith(<span style="color: #a31515;">"ftp"</span>))
        {
            Url = <span style="color: #a31515;">"http://" </span>+ Url;
        }
        <span style="color: blue;">var </span>request = <span style="color: #2b91af;">WebRequest</span>.Create(<span style="color: #a31515;">"http://tinyurl.com/api-create.php?url=" </span>+ Url);
        <span style="color: blue;">var </span>res = request.GetResponse();
        <span style="color: blue;">string </span>text;
        <span style="color: blue;">using </span>(<span style="color: blue;">var </span>reader = <span style="color: blue;">new </span><span style="color: #2b91af;">StreamReader</span>(res.GetResponseStream()))
        {
            text = reader.ReadToEnd();
        }
        <span style="color: blue;">return </span>text;
    }
    <span style="color: blue;">catch </span>(<span style="color: #2b91af;">Exception</span>)
    {
        <span style="color: blue;">return </span>Url;
    }
}</pre>
<p>Hope this can as useful to you as it was for me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/05/c-tinyurl-conversion-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert LiNQ resultset to DataTable</title>
		<link>http://www.failboy.net/2009/05/convert-linq-resultset-to-datatable/</link>
		<comments>http://www.failboy.net/2009/05/convert-linq-resultset-to-datatable/#comments</comments>
		<pubDate>Wed, 06 May 2009 12:22:27 +0000</pubDate>
		<dc:creator>FailBoy</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[ExtensionMethod]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.failboy.net/?p=5</guid>
		<description><![CDATA[When the LiNQ to SQL framework was originally released in BETA there was a built in extension method to convert the LiNQ result to either a DataTable or DataSet if I remember correctly. However when it came to release this disappeared for some strange reason. Now I&#8217;m sure MS had their reasons for removing it [...]]]></description>
			<content:encoded><![CDATA[<p>When the LiNQ to SQL framework was originally released in BETA there was a built in extension method to convert the LiNQ result to either a DataTable or DataSet if I remember correctly. However when it came to release this disappeared for some strange reason.</p>
<p>Now I&#8217;m sure MS had their reasons for removing it but I don&#8217;t care and want it back <img src='http://www.failboy.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  So I&#8217;ve written a little extension method to convert the LiNQ resultset to a DataTable using a bit of Reflection. Here&#8217;s the code and enjoy! As always, I take no responsibility for its usage <img src='http://www.failboy.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<blockquote><p>/// &lt;summary&gt;<br />
/// Converts an IEnumerable type collection into a DataTable<br />
/// &lt;/summary&gt;<br />
/// &lt;param name=&#8221;collection&#8221;&gt;Collection type that implements IEnumberable&lt;/param&gt;<br />
/// &lt;returns&gt;Datatable representing IEnumerable collection&lt;/returns&gt;<br />
public static DataTable ToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt;collection)<br />
{<br />
// Create DataTable to Fill<br />
DataTable _newDataTable = new DataTable();</p>
<p>// Retrieve the Type passed into the Method<br />
Type _impliedType = typeof(T);</p>
<p>//Get an array of the Type&#8217;s properties<br />
PropertyInfo[] _propInfo = _impliedType.GetProperties();</p>
<p>//Create the columns in the DataTable<br />
foreach (PropertyInfo pi in _propInfo)<br />
{<br />
_newDataTable.Columns.Add(pi.Name, pi.PropertyType);<br />
}</p>
<p>//Populate the table<br />
foreach (T item in collection)<br />
{<br />
DataRow _newDataRow = _newDataTable.NewRow();<br />
_newDataRow.BeginEdit();</p>
<p>foreach (PropertyInfo pi in _propInfo)<br />
{<br />
_newDataRow[pi.Name] = pi.GetValue(item, null);<br />
}</p>
<p>_newDataRow.EndEdit();<br />
_newDataTable.Rows.Add(_newDataRow);<br />
}</p>
<p>return _newDataTable;<br />
}</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.failboy.net/2009/05/convert-linq-resultset-to-datatable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
