<?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; DataTable</title>
	<atom:link href="http://www.failboy.net/tag/datatable/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>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>
