<?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>Schotime.net &#187; Extension Methods</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/extension-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Sun, 20 Nov 2011 01:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Importing Data Files with Linq</title>
		<link>http://schotime.net/blog/index.php/2008/03/18/importing-data-files-with-linq/</link>
		<comments>http://schotime.net/blog/index.php/2008/03/18/importing-data-files-with-linq/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 12:21:58 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/03/18/importing-data-files-with-linq/</guid>
		<description><![CDATA[In my previous Linq post I discussed using Linq with Regular expressions and how much less code was needed. In this post we&#8217;ll again see how Linq can be used to speed up and simplify development. There are many situations where you need to read data from a file into memory or a database. Lets [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous <a href="http://schotime.net/index.php/2008/03/10/linq-and-regular-expressions/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/schotime.net/index.php/2008/03/10/linq-and-regular-expressions/?referer=');">Linq post</a> I discussed using Linq with Regular expressions and how much less code was needed. In this post we&#8217;ll again see how Linq can be used to speed up and simplify development.</p>
<p>There are many situations where you need to read data from a file into memory or a database. Lets consider this scenario.</p>
<p>We have a tab delimited data file that has been exported from some web or windows application etc. Below is the sample file.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code">carrots    4    0.64
beans      3    0.12
oranges    7    1.02
apples     5    0.87</pre>
</td>
</tr>
</tbody>
</table>
<p>This file includes three columns. Column 1is the name of the product. Column 2 is the quantity and Column 3 is the unit price. We would also like a total price column derived from column 2 being multiplied by column 3.</p>
<p>Our aim here is to load these into our application programmatically and then bind them to a gridview. Normally you would have to read the file in line by line then do a split on each line, enter each line into a prebuilt class or datatable then add a new column and multiply the quantity by the price to obtain the total. However by using a nice little extension helper method, all of this can be rolled into just a few lines of code.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">public static </span><span style="color: #2b91af;">IEnumerable</span>&lt;<span style="color: blue;">string</span>&gt; ReadLinesFromFile(<span style="color: blue;">string </span>filename)
{
    <span style="color: blue;">using </span>(<span style="color: #2b91af;">StreamReader </span>reader = <span style="color: blue;">new </span><span style="color: #2b91af;">StreamReader</span>(filename))
    {
        <span style="color: blue;">while </span>(<span style="color: blue;">true</span>)
        {
            <span style="color: blue;">string </span>s = reader.ReadLine();
            <span style="color: blue;">if </span>(s == <span style="color: blue;">null</span>)
                <span style="color: blue;">break</span>;
            <span style="color: blue;">yield return </span>s;
        }
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>Above is the extension method to convert lines of a file into a IEnumerable list of strings.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">var </span>products = <span style="color: blue;">from </span>line <span style="color: blue;">in </span>ReadLinesFromFile(<span style="color: #a31515;">@"c:\import.txt"</span>)
               <span style="color: blue;">let </span>item = line.Split(<span style="color: blue;"></span><span style="color: #a31515;">'\t'</span>)
               <span style="color: blue;">select new
               </span>{
                   Product = item[0],
                   Quantity = <span style="color: #2b91af;">Convert</span>.ToInt32(item[1]),
                   Price = <span style="color: #2b91af;">Convert</span>.ToDecimal(item[2]),
                   Total = <span style="color: #2b91af;">Convert</span>.ToInt32(item[1]) * <span style="color: #2b91af;">Convert</span>.ToDecimal(item[2])
               };</pre>
<pre class="code">GridView1.DataSource = products;
GridView1.DataBind();</pre>
</td>
</tr>
</tbody>
</table>
<p>What this gives us is a list of products, with each one having 4 properties associated with it. We can then bind this straight to a gridview by setting the datasource of the gridview to products and calling the DataBind() method. This will display a table with 4 columns with the headers Product, Quantity, Price and Total.</p>
<p>The Linq statement can then be modified in many ways to accommodate different file formats (eg. CSV or fixed width) with a few simple changes.</p>
<p>This is just another way Linq is making things easier!</p>
<p>Schotime</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/03/18/importing-data-files-with-linq/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

