<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rui Jarimba</title>
	<atom:link href="http://ruijarimba.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ruijarimba.wordpress.com</link>
	<description>Yet another geek blog about software development</description>
	<lastBuildDate>Tue, 21 May 2013 09:40:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ruijarimba.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/8f2d4b3b06675f44143141615a554749?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Rui Jarimba</title>
		<link>http://ruijarimba.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ruijarimba.wordpress.com/osd.xml" title="Rui Jarimba" />
	<atom:link rel='hub' href='http://ruijarimba.wordpress.com/?pushpress=hub'/>
		<item>
		<title>2012 in review</title>
		<link>http://ruijarimba.wordpress.com/2012/12/31/2012-in-review/</link>
		<comments>http://ruijarimba.wordpress.com/2012/12/31/2012-in-review/#comments</comments>
		<pubDate>Mon, 31 Dec 2012 01:29:33 +0000</pubDate>
		<dc:creator>Rui Jarimba</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://ruijarimba.wordpress.com/?p=672</guid>
		<description><![CDATA[The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog. Here&#8217;s an excerpt: 4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 24,000 views in 2012. If each view were a film, this &#8230; <a href="http://ruijarimba.wordpress.com/2012/12/31/2012-in-review/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=672&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.</p>
<p>	<a href="http://ruijarimba.wordpress.com/2012/annual-report/"><img src="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/2012-emailteaser.png" width="100%" alt="" /></a></p>
<p>Here&#8217;s an excerpt:</p>
</p>
<blockquote><p>4,329 films were submitted to the 2012 Cannes Film Festival. This blog had <strong>24,000</strong> views in 2012. If each view were a film, this blog would power 6 Film Festivals</p></blockquote>
<p><a href="http://ruijarimba.wordpress.com/2012/annual-report/">Click here to see the complete report.</a></p>
<br />Filed under: <a href='http://ruijarimba.wordpress.com/category/net/'>.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruijarimba.wordpress.com/672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruijarimba.wordpress.com/672/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=672&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ruijarimba.wordpress.com/2012/12/31/2012-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/758a1b8e56090cab54a177355b391d3d?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ruijarimba</media:title>
		</media:content>

		<media:content url="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/2012-emailteaser.png" medium="image" />
	</item>
		<item>
		<title>Improving LINQ code reusability: Select method</title>
		<link>http://ruijarimba.wordpress.com/2012/11/25/improving-linq-code-reusability-select-method/</link>
		<comments>http://ruijarimba.wordpress.com/2012/11/25/improving-linq-code-reusability-select-method/#comments</comments>
		<pubDate>Sun, 25 Nov 2012 14:48:58 +0000</pubDate>
		<dc:creator>Rui Jarimba</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://ruijarimba.wordpress.com/?p=633</guid>
		<description><![CDATA[Select method is used to project each element of a sequence into a new form, i.e. it can be used to map a collection of one type to a collection of another type. In this article I&#8217;ll show you a &#8230; <a href="http://ruijarimba.wordpress.com/2012/11/25/improving-linq-code-reusability-select-method/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=633&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Select method is used to project each element of a sequence into a new form, i.e. it can be used to map a collection of one type to a collection of another type. In this article I&#8217;ll show you a simple approach that will allow you to reuse the code used in the Select method.</p>
<p><strong>Table of contents</strong></p>
<ul>
<li><a title="The problem" href="#section1">The problem</a></li>
<li><a title="Solution 1 - Creating a method for the mapping" href="#section2">Solution 1 &#8211; Creating a method for the mapping</a></li>
<li><a title="Solution 2 - Creating a generic ObjectMapper object" href="#section3">Solution 2 &#8211; Creating a generic ObjectMapper object</a></li>
<ul>
<li><a title="Example 1: Using LINQ" href="#section1-1">Example 1: Using LINQ</a></li>
<li><a title="Example 2: Mapping a single object" href="#section1-2">Example 2: Mapping a single object</a></li>
<li><a title="Example 2: Unit testing" href="#section1-3">Unit testing</a></li>
</ul>
<li><a title="Final thoughts" href="#section4">Final thoughts</a></li>
<li><a href="#section-references">References</a></li>
<li><a href="#section-downloads">Downloads</a></li>
</ul>
<p><a style="color:#fff;margin-top:20px;" name="section1"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">The Problem</span></h2>
<p>Consider the following model:</p>
<p><img class="aligncenter size-full wp-image-636" title="domain" alt="" src="http://ruijarimba.files.wordpress.com/2012/11/domain.png?w=584&#038;h=364" height="364" width="584" /></p>
<p>Let&#8217;s suppose that you have a services layer, so you don&#8217;t want to expose your domain objects directly to the client applications. Instead you create a set of data contracts (or DTOs, if you prefer):</p>
<p><a href="http://ruijarimba.files.wordpress.com/2012/11/data-contracts1.png"><img class="aligncenter size-full wp-image-663" title="data-contracts" alt="" src="http://ruijarimba.files.wordpress.com/2012/11/data-contracts1.png?w=584"   /></a></p>
<p>At some stage you&#8217;ll have to convert those Domain objects to data contracts. This is a common way of doing it:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
var details = repository.All&lt;Album&gt;().Select(album =&gt; new AlbumDetail {
    AlbumId = album.AlbumId,
    Price = album.Price,
    Title = album.Title,

    ArtistId = album.ArtistId,
    GenreId = album.GenreId,
    ArtistName = (album.Artist == null) ? null : album.Artist.Name,
    GenreName = (album.Genre == null) ? null : album.Genre.Name
});
</pre>
<p>There is a problem with this approach &#8211; if you need to query the same collection but using different criteria you have to duplicate the code inside the Select method.</p>
<p><a style="color:#fff;margin-top:20px;" name="section2"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Solution 1 &#8211; Creating a method for the mapping</span></h2>
<p>In order to reuse the code, we can create a method that converts Album objects (Domain) to data contract objects:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
private static AlbumSummary CreateAlbumSummary(Album album)
{
    return new AlbumSummary {
        AlbumId = album.AlbumId,
        Title = album.Title,

        ArtistName = (album.Artist == null) ? null : album.Artist.Name
    };
}

private static AlbumDetail CreateAlbumDetail(Album album)
{
    return new AlbumDetail {
        AlbumId = album.AlbumId,
        Price = album.Price,
        Title = album.Title,

        ArtistId = album.ArtistId,
        GenreId = album.GenreId,
        ArtistName = (album.Artist == null) ? null : album.Artist.Name,
        GenreName = (album.Genre == null) ? null : album.Genre.Name
    };
}
</pre>
<p>Using the code:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
var albums = Albums.Select(CreateAlbumDetail);
var albumsByGenre = Albums.Where(x =&gt; x.GenreId == genreId).Select(CreateAlbumDetail);

// alternative way
var albums2 = Albums.Select(x =&gt; CreateAlbumDetail(x));
var albumsByGenre2 = Albums.Where(x =&gt; x.GenreId == genreId).Select(x =&gt; CreateAlbumDetail(x));
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section3"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Solution 2 &#8211; Creating a generic ObjectMapper object</span></h2>
<p>The previous solution solves the code reusability problem, but there&#8217;s still a tight coupling between components. Abstractions should be used to implement loose coupling between components &#8211; in this case, to abstract the mapping code.</p>
<p>Step 1: define a contract (interface) with a method that converts one object of type TSource to an object of type TDestination:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface IObjectMapper
{
    TDestination Map&lt;TSource, TDestination&gt;(TSource source);
}
</pre>
<p>Step 2: create a class that implements IObjectMapper (click to expand):</p>
<pre class="brush: csharp; collapse: true; light: false; title: ; toolbar: true; wrap-lines: false; notranslate">
public class ObjectMapper : IObjectMapper
{
    private Dictionary&lt;Type, Func&lt;object, object&gt;&gt; Mappers = new Dictionary&lt;Type, Func&lt;object, object&gt;&gt;
    {
        { typeof(Tuple&lt;Album, AlbumDetail&gt;), CreateAlbumDetail },
        { typeof(Tuple&lt;Album, AlbumSummary&gt;), CreateAlbumSummary }

        // more mappings here
        // ....
    };


    public TDestination Map&lt;TSource, TDestination&gt;(TSource source)
    {
        if(source == null)
            return default(TDestination);

        Func&lt;object, object&gt; mapper = null;
        Type key = typeof(Tuple&lt;TSource, TDestination&gt;);

        if(Mappers.TryGetValue(key, out mapper))
        {
            var newObject = mapper(source);
            return (TDestination) newObject;
        }

        string errorMessage = string.Format(&quot;Invalid mapping (Source: {0}, Destination: {1})&quot;;,
                                            typeof(TSource).FullName, 
                                            typeof(TDestination).FullName);
        
        throw new InvalidOperationException(errorMessage);
    }


    private static object CreateAlbumDetail(object source)
    {
        var album = source as Album;

        return new AlbumDetail {
            AlbumId = album.AlbumId,
            Price = album.Price,
            Title = album.Title,

            ArtistId = album.ArtistId,
            GenreId = album.GenreId,
            ArtistName = (album.Artist == null) ? null : album.Artist.Name,
            GenreName = (album.Genre == null) ? null : album.Genre.Name
        };
    }

    private static object CreateAlbumSummary(object source)
    {
        var album = source as Album;

        return new AlbumSummary {
            AlbumId = album.AlbumId,
            Title = album.Title,
            
            ArtistName = (album.Artist == null) ? null : album.Artist.Name
        };
    }
}
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-1"></a></p>
<h3 style="font-size:12pt;"><strong>Example 1: Using LINQ</strong></h3>
<p>Using the mapper in a LINQ expression &#8211; convert an Album collection to an AlbumSummary collection:</p>
<pre class="brush: csharp; title: ; notranslate">
IObjectMapper mapper = new ObjectMapper();

IEnumerable&lt;AlbumSummary&gt; summaries = repository.All&lt;Album&gt;()
                                        .Select(mapper.Map&lt;Album, AlbumSummary&gt;);
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-2"></a></p>
<h3 style="font-size:12pt;"><strong>Example 1: Mapping a single object</strong></h3>
<p>Using the mapper for a single object:</p>
<pre class="brush: csharp; title: ; notranslate">
var album = new Album {
    AlbumId = 1,
    Price = 10.0m,
    Title = &quot;The Dreamer&quot;,
    Artist = new Artist { ArtistId = 1, Name = &quot;José James&quot; },
    Genre = new Genre { GenreId = 1, Name = &quot;Jazz&quot; }
};

IObjectMapper mapper = new ObjectMapper();

AlbumDetail albumDetail = mapper.Map&lt;Album, AlbumDetail&gt;(album);
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-3"></a></p>
<h3 style="font-size:12pt;"><strong>Unit Testing</strong></h3>
<p>Some NUnit tests:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
[Test]
public void Given_a_non_existing_mapping_when_mapping_object_then_should_throw_InvalidOperationException()
{
    // arrange
    IObjectMapper mapper = new ObjectMapper();
    var albumDetail = new AlbumDetail();

    // act/assert
    Assert.Throws&lt;InvalidOperationException&gt;(() =&gt; 
        // non-existing mapping
        mapper.Map&lt;AlbumDetail, AlbumSummary&gt;(albumDetail)
    );
}

[Test]
public void Given_an_album_when_mapping_to_album_summary_should_equals_expected_album_summary()
{
    // arrange
    IObjectMapper mapper = new ObjectMapper();
    
    var album = new Album {
        AlbumId = 4,
        Price = 10.0m,
        Title = &quot;Heritage&quot;,
        Artist = new Artist { ArtistId = 4, Name = &quot;Opeth&quot; },
        Genre = new Genre { GenreId = 4, Name = &quot;Metal&quot; }
    };

    var expectedAlbumSummary = new AlbumSummary {
        AlbumId = 4,
        ArtistName = &quot;Opeth&quot;,
        Title = &quot;Heritage&quot;
    };
    
    // act
    AlbumSummary albumSummary = mapper.Map&lt;Album, AlbumSummary&gt;(album);
    
    // assert
    Assert.AreEqual(albumSummary, expectedAlbumSummary);
}
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section4"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Final thoughts</span></h2>
<p>In this article you learned how to reuse the code used in the Select method, and how you can use that code to map single objects. But writing mapping code is tedious and time consuming. There are mapping tools out there that can make your life easier &#8211; <a href="https://github.com/AutoMapper/AutoMapper" title="AutoMapper">AutoMapper</a> is one of them. I&#8217;ve used it in the past and I definitely recommend it. So, why use Automapper? Quoting their website:</p>
<p><em>&#8220;What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper&#8217;s established convention, almost zero configuration is needed to map two types&#8221;</em></p>
<p><em>&#8220;Mapping code is boring. Testing mapping code is even more boring. AutoMapper provides simple configuration of types, as well as simple testing of mappings&#8221;</em></p>
<p><a style="color:#fff;margin-top:20px;" name="section-references"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">References</span></h2>
<ul>
<li><a title="Enumerable.Select Method [MSDN]" href="http://msdn.microsoft.com/en-us/library/bb357126.aspx" target="_blank">Enumerable.Select Method [MSDN]</a></li>
<li><a title="Dependency Inversion principle" href="http://en.wikipedia.org/wiki/Dependency_inversion_principle" target="_blank">Dependency Inversion principle</a></li>
<li><a title="Getting started - AutoMapper" href="https://github.com/AutoMapper/AutoMapper/wiki/Getting-started" target="_blank">Getting started &#8211; AutoMapper</a></li>
</ul>
<p><a style="color:#fff;margin-top:20px;" name="section-downloads"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Downloads</span></h2>
<p>Download the demo project (VS2010): <a title="LINQ-Select.zip" href="https://docs.google.com/uc?id=0B411XiA1vaSzdVlzTnRZeXh1cTg&amp;export=download">LINQ-Select.zip</a></p>
<div style="float:none;display:inline;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/C%23" rel="tag">C#</a>, <a href="http://technorati.com/tags/.NET" rel="tag">.NET</a>, <a href="http://technorati.com/tags/LINQ" rel="tag">LINQ</a>, <a href="http://technorati.com/tags/Dependency+Injection" rel="tag">Dependency Injection</a></div>
<p><a href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=2431313" style="display:none;" rel="tag">CodeProject</a></p>
<br />Filed under: <a href='http://ruijarimba.wordpress.com/category/net/'>.NET</a>, <a href='http://ruijarimba.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://ruijarimba.wordpress.com/category/design-patterns/'>Design Patterns</a>, <a href='http://ruijarimba.wordpress.com/category/unit-testing/'>Unit Testing</a> Tagged: <a href='http://ruijarimba.wordpress.com/tag/net/'>.NET</a>, <a href='http://ruijarimba.wordpress.com/tag/c/'>C#</a>, <a href='http://ruijarimba.wordpress.com/tag/dependency-injection/'>Dependency Injection</a>, <a href='http://ruijarimba.wordpress.com/tag/linq/'>LINQ</a>, <a href='http://ruijarimba.wordpress.com/tag/unit-testing-2/'>Unit testing</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruijarimba.wordpress.com/633/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruijarimba.wordpress.com/633/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=633&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ruijarimba.wordpress.com/2012/11/25/improving-linq-code-reusability-select-method/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/758a1b8e56090cab54a177355b391d3d?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ruijarimba</media:title>
		</media:content>

		<media:content url="http://ruijarimba.files.wordpress.com/2012/11/domain.png" medium="image">
			<media:title type="html">domain</media:title>
		</media:content>

		<media:content url="http://ruijarimba.files.wordpress.com/2012/11/data-contracts1.png" medium="image">
			<media:title type="html">data-contracts</media:title>
		</media:content>
	</item>
		<item>
		<title>No comment</title>
		<link>http://ruijarimba.wordpress.com/2012/08/14/no-comment/</link>
		<comments>http://ruijarimba.wordpress.com/2012/08/14/no-comment/#comments</comments>
		<pubDate>Tue, 14 Aug 2012 13:27:13 +0000</pubDate>
		<dc:creator>Rui Jarimba</dc:creator>
				<category><![CDATA[WTF]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database design]]></category>
		<category><![CDATA[No Comment]]></category>

		<guid isPermaLink="false">http://ruijarimba.wordpress.com/?p=621</guid>
		<description><![CDATA[This table was in a database of a company where I worked some years ago. I can&#8217;t think of any reason someone would create a table like this one, can you? Filed under: WTF Tagged: Database, Database design, No Comment, &#8230; <a href="http://ruijarimba.wordpress.com/2012/08/14/no-comment/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=621&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This table was in a database of a company where I worked some years ago.</p>
<p><a href="http://ruijarimba.files.wordpress.com/2012/08/tbl_reference.png"><img class="size-full wp-image aligncenter" src="http://ruijarimba.files.wordpress.com/2012/08/tbl_reference.png?w=287" alt="Image" /></a>I can&#8217;t think of any reason someone would create a table like this one, can you?</p>
<br />Filed under: <a href='http://ruijarimba.wordpress.com/category/wtf/'>WTF</a> Tagged: <a href='http://ruijarimba.wordpress.com/tag/database/'>Database</a>, <a href='http://ruijarimba.wordpress.com/tag/database-design/'>Database design</a>, <a href='http://ruijarimba.wordpress.com/tag/no-comment/'>No Comment</a>, <a href='http://ruijarimba.wordpress.com/tag/wtf/'>WTF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruijarimba.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruijarimba.wordpress.com/621/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=621&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ruijarimba.wordpress.com/2012/08/14/no-comment/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/758a1b8e56090cab54a177355b391d3d?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ruijarimba</media:title>
		</media:content>

		<media:content url="http://ruijarimba.files.wordpress.com/2012/08/tbl_reference.png?w=287" medium="image">
			<media:title type="html">Image</media:title>
		</media:content>
	</item>
		<item>
		<title>Bulk Insert in .NET applications, part 1</title>
		<link>http://ruijarimba.wordpress.com/2012/03/25/bulk-insert-dot-net-applications-part1/</link>
		<comments>http://ruijarimba.wordpress.com/2012/03/25/bulk-insert-dot-net-applications-part1/#comments</comments>
		<pubDate>Sun, 25 Mar 2012 21:00:11 +0000</pubDate>
		<dc:creator>Rui Jarimba</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Bulk Insert]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SqlBulkCopy]]></category>

		<guid isPermaLink="false">http://ruijarimba.wordpress.com/?p=575</guid>
		<description><![CDATA[This is the first of a 2 series articles about how to perform bulk inserts in your .NET applications, using a SQL Server database. In this article I&#8217;ll show how to create a wrapper object for SqlBulkCopy that can do &#8230; <a href="http://ruijarimba.wordpress.com/2012/03/25/bulk-insert-dot-net-applications-part1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=575&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is the first of a 2 series articles about how to perform bulk inserts in your .NET applications, using a SQL Server database.</p>
<p>In this article I&#8217;ll show how to create a wrapper object for SqlBulkCopy that can do a bulk insert for a collection of objects. In the second article I&#8217;ll show how that wrapper can be easily integrated with Entity Framework (creating extension methods for DbContext and ObjectContext).</p>
<p><strong>Table of contents</strong></p>
<ul>
<li><a title="The problem" href="#section1">The problem</a>
<li><a title="The solution" href="#section2">The solution</a>
<li><a title="Using the code" href="#section4">Using the code</a>
<ul>
<li><a title="Example 1: Basic usage" href="#section1-1">1. Basic usage</a></li>
<li><a title="Example 2: Specifying batch size and bulk options" href="#section1-2">2. Specifying batch size and bulk options</a></li>
<li><a title="Using column mappings" href="#section1-3">3. Using column mappings</a></li>
<li><a title="Filtering properties to be copied" href="#section1-4">4. Filtering properties to be copied</a></li>
</ul>
</li>
<li><a href="#section-references">References</a></li>
<li><a href="#section-downloads">Downloads</a></li>
</ul>
<p><a style="color:#fff;margin-top:20px;" name="section1"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">The problem</span></h2>
<p>I am working in an application that parses Excel files and creates an object for each line. After some validations and properties mapping the objects are then being inserted in a database table, one by one. At the beggining this wasn&#8217;t a big problem because the number of objects to insert in the database was small, but now there can be files with thousands of rows, so using Entity Framework isn&#8217;t the best way to do it (currently Entity Framework has no support for bulk insert operations).</p>
<p><a style="color:#fff;margin-top:20px;" name="section2"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">The solution</span></h2>
<p>I decided to use the object <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy%28v=vs.80%29.aspx" title="[MSDN] SqlBulkCopy" target="_blank">SqlBulkCopy</a> because it seemed the best and easiest option for performing a bulk insert in a SQL Server database. I created a wrapper for SqlBulkCopy &#8211; BulkCopy.cs. This class works the same way as SqlBulkCopy but has some extra features.</p>
<p><a href="https://ruijarimba.files.wordpress.com/2012/03/bulkcopy-cs1.png"><img src="https://ruijarimba.files.wordpress.com/2012/03/bulkcopy-cs1.png?w=584&#038;h=170" alt="" title="BulkCopy.cs" width="584" height="170" class="aligncenter size-full wp-image-587" /></a></p>
<p>The following properties are available: </p>
<ul>
<li><strong>DestinationTableName</strong>: Name of the destination table on the server</li>
<li><strong>BatchSize</strong> (optional): Number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server</li>
<li><strong>ConnectionString</strong>: Database connection string</li>
<li><strong>ExpressionFilter</strong>: Filters the properties to be included</li>
</ul>
<p>And these are the methods available (see examples below):</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
public void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items) where T : class;
public void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items, SqlBulkCopyOptions options) where T : class;
public void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items, SqlBulkCopyOptions options, IEnumerable&lt;SqlBulkCopyColumnMapping&gt; columnMappings) where T : class;
</pre>
<p>BulkCopy.cs source code (click to expand):</p>
<pre class="brush: csharp; collapse: true; light: false; title: ; toolbar: true; wrap-lines: false; notranslate">
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using BulkCopy.Extensions;

namespace BulkCopy
{
    /// &lt;summary&gt;
    /// Lets you efficiently bulk load a SQL Server table with data from another source.
    /// This is a wrapper class for &lt;see cref=&quot;SqlBulkCopy&quot;/&gt;
    /// &lt;/summary&gt;
    public class BulkCopy
    {
        /// &lt;summary&gt;
        /// Name of the destination table on the server
        /// &lt;/summary&gt;
        public string DestinationTableName { get; set; }

        /// &lt;summary&gt;
        /// Number of rows in each batch. 
        /// At the end of each batch, the rows in the batch are sent to the server.
        /// &lt;/summary&gt;
        public int? BatchSize { get; set; }

        /// &lt;summary&gt;
        /// Database connection string
        /// &lt;/summary&gt;
        public string ConnectionString { get; set; }

        /// &lt;summary&gt;
        /// Filters the properties to be included
        /// &lt;/summary&gt;
        public Func&lt;PropertyDescriptor, bool&gt; ExpressionFilter { get; set; }

		
		
        /// &lt;summary&gt;
        /// Initializes a new instance of the &lt;see cref=&quot;BulkCopy&amp;lt;T&amp;gt;&quot;/&gt; class.
        /// &lt;/summary&gt;
        public BulkCopy()
        {
        }

        /// &lt;summary&gt;
        /// Initializes a new instance of the &lt;see cref=&quot;BulkCopy&amp;lt;T&amp;gt;&quot;/&gt; class.
        /// &lt;/summary&gt;
        public BulkCopy(string connectionString)
        {
            this.ConnectionString = connectionString;
        }

		
		
        /// &lt;summary&gt;
        /// Copies all items in a collection to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;The items that will be copied to the destination table&lt;/param&gt;
        /// &lt;param name=&quot;options&quot;&gt;A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. &lt;see cref=&quot;SqlBulkCopyOptions&quot;/&gt;&lt;/param&gt;
        public virtual void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items) where T : class
        {
            WriteToServer(items, SqlBulkCopyOptions.Default);
        }

        /// &lt;summary&gt;
        /// Copies all items in a collection to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;The items that will be copied to the destination table&lt;/param&gt;
        /// &lt;param name=&quot;options&quot;&gt;A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. &lt;see cref=&quot;SqlBulkCopyOptions&quot;/&gt;&lt;/param&gt;
        public virtual void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items, SqlBulkCopyOptions options) where T : class
        {
            DataTable dataTable = (this.ExpressionFilter == null) ? items.ToDataTable() : items.ToDataTable(this.ExpressionFilter);

            WriteToServer(dataTable, options);
        }

        /// &lt;summary&gt;
        /// Copies all items in a collection to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;The items that will be copied to the destination table&lt;/param&gt;
        /// &lt;param name=&quot;options&quot;&gt;A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. &lt;see cref=&quot;SqlBulkCopyOptions&quot;/&gt;&lt;/param&gt;
        /// &lt;param name=&quot;columnMappings&quot;&gt;Returns a collection of System.Data.SqlClient.SqlBulkCopyColumnMapping items. 
        /// Column mappings define the relationships between columns in the data source and columns in the destination.&lt;/param&gt;
        public virtual void WriteToServer&lt;T&gt;(IEnumerable&lt;T&gt; items, SqlBulkCopyOptions options, IEnumerable&lt;SqlBulkCopyColumnMapping&gt; columnMappings) where T : class
        {
            DataTable dataTable = (this.ExpressionFilter == null) ? items.ToDataTable() : items.ToDataTable(this.ExpressionFilter);

            WriteToServer(dataTable, options, columnMappings);
        }

        /// &lt;summary&gt;
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;A System.Data.DataTable whose rows will be copied to the destination table&lt;/param&gt;
        private void WriteToServer(DataTable dataTable)
        {
            WriteToServer(dataTable, SqlBulkCopyOptions.Default);
        }

        /// &lt;summary&gt;
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;A System.Data.DataTable whose rows will be copied to the destination table&lt;/param&gt;
        /// &lt;param name=&quot;options&quot;&gt;A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. &lt;see cref=&quot;SqlBulkCopyOptions&quot;/&gt;&lt;/param&gt;
        private void WriteToServer(DataTable dataTable, SqlBulkCopyOptions options)
        {
            var columnMappings = from x in dataTable.Columns.Cast&lt;DataColumn&gt;()
                                 select new SqlBulkCopyColumnMapping(x.ColumnName, x.ColumnName);

            WriteToServer(dataTable, options, columnMappings);
        }

        /// &lt;summary&gt;
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;dataTable&quot;&gt;A System.Data.DataTable whose rows will be copied to the destination table&lt;/param&gt;
        /// &lt;param name=&quot;options&quot;&gt;A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. &lt;see cref=&quot;SqlBulkCopyOptions&quot;/&gt;&lt;/param&gt;
        /// &lt;param name=&quot;columnMappings&quot;&gt;Returns a collection of System.Data.SqlClient.SqlBulkCopyColumnMapping items. 
        /// Column mappings define the relationships between columns in the data source and columns in the destination.&lt;/param&gt;
        private void WriteToServer(DataTable dataTable, SqlBulkCopyOptions options, IEnumerable&lt;SqlBulkCopyColumnMapping&gt; columnMappings)
        {
            // table name matching:
            // checks for DestinationTableName value
            // if null or empty, checks for dataTable.TableName
            string destinationTableName =
                (string.IsNullOrWhiteSpace(DestinationTableName) ? null : DestinationTableName)
                ?? (string.IsNullOrWhiteSpace(dataTable.TableName) ? null : dataTable.TableName);

            if(string.IsNullOrWhiteSpace(destinationTableName))
                throw new ArgumentException(&quot;destinationTableName cannot be null or empty&quot;);

            using(var bulkCopy = new SqlBulkCopy(this.ConnectionString, options))
            {
                bulkCopy.DestinationTableName = destinationTableName;

                if(this.BatchSize.HasValue)
                    bulkCopy.BatchSize = this.BatchSize.Value;

                foreach(var mapping in columnMappings)
                    bulkCopy.ColumnMappings.Add(mapping);

                bulkCopy.WriteToServer(dataTable);
            }
        }
    }
}
</pre>
<p>The BulkCopy object is using internally some extension methods that converts a collection of objects to a DataTable. (taken from SO&#8217;s post <a href="http://stackoverflow.com/a/5805044" title="Generic List to DataTable" target="_blank">Generic List to DataTable</a>, with some small modifications).</p>
<p>Click to expand the source code:</p>
<pre class="brush: csharp; collapse: true; light: false; title: ; toolbar: true; wrap-lines: false; notranslate">
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace BulkCopy.Extensions
{
    public static class DataExtensions
    {
        /// &lt;summary&gt;
        /// Basic data types 
        /// &lt;/summary&gt;
        private static Type[] dataTypes = new[] {
            typeof(byte)
            ,typeof(sbyte)
            ,typeof(short)
            ,typeof(ushort)
            ,typeof(int)
            ,typeof(uint)
            ,typeof(long)
            ,typeof(ulong)
            ,typeof(float)
            ,typeof(double)
            ,typeof(decimal)
            ,typeof(bool)
            ,typeof(char)
            ,typeof(Guid)
            ,typeof(DateTime)
            ,typeof(DateTimeOffset)
            ,typeof(byte[])
            ,typeof(string)
        };

        /// &lt;summary&gt;
        /// Converts a generic List to a DataTable
        /// &lt;see cref=&quot;http://stackoverflow.com/a/5805044&quot;/&gt;
        /// &lt;/summary&gt;
        /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object to convert to DataTable&lt;/typeparam&gt;
        /// &lt;param name=&quot;data&quot;&gt;Data to be converted&lt;/param&gt;
        /// &lt;returns&gt;The converted DataTable&lt;/returns&gt;
        public static DataTable ToDataTable&lt;T&gt;(this IList&lt;T&gt; data)
        {
            IEnumerable&lt;PropertyDescriptor&gt; properties = from x in TypeDescriptor.GetProperties(typeof(T)).Cast&lt;PropertyDescriptor&gt;()
                                                         where IsBasicType(x.PropertyType)
                                                         select x;

            DataTable table = GetDataTable(data, properties);
            return table;
        }

        /// &lt;summary&gt;
        /// Converts a generic List to a DataTable
        /// &lt;see cref=&quot;http://stackoverflow.com/a/5805044&quot;/&gt;
        /// &lt;/summary&gt;
        /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object to convert to DataTable&lt;/typeparam&gt;
        /// &lt;param name=&quot;data&quot;&gt;Data to be converted&lt;/param&gt;
        /// &lt;returns&gt;The converted DataTable&lt;/returns&gt;
        public static DataTable ToDataTable&lt;T&gt;(this IList&lt;T&gt; data, Func&lt;PropertyDescriptor, bool&gt; expression)
        {
            var properties = TypeDescriptor.GetProperties(typeof(T))
                .Cast&lt;PropertyDescriptor&gt;()
                .Where(expression);

            DataTable table = GetDataTable(data, properties);
            return table;
        }

        /// &lt;summary&gt;
        /// Converts an IEnumerable to a DataTable
        /// &lt;see cref=&quot;http://stackoverflow.com/a/5805044&quot;/&gt;
        /// &lt;/summary&gt;
        /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object to convert to DataTable&lt;/typeparam&gt;
        /// &lt;param name=&quot;data&quot;&gt;Data to be converted&lt;/param&gt;
        /// &lt;returns&gt;The DataTable&lt;/returns&gt;
        public static DataTable ToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; data)
        {
            return data.ToList().ToDataTable();
        }

        /// &lt;summary&gt;
        /// Converts an IEnumerable to a DataTable
        /// &lt;see cref=&quot;http://stackoverflow.com/a/5805044&quot;/&gt;
        /// &lt;/summary&gt;
        /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object to convert to DataTable&lt;/typeparam&gt;
        /// &lt;param name=&quot;data&quot;&gt;Data to be converted&lt;/param&gt;
        /// &lt;param name=&quot;expression&quot;&gt;Predicate to filter the properties of &lt;typeparamref name=&quot;T&quot;/&gt; to be included to the DataTable&lt;/param&gt;
        /// &lt;returns&gt;The DataTable&lt;/returns&gt;
        public static DataTable ToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; data, Func&lt;PropertyDescriptor, bool&gt; expression)
        {
            return data.ToList().ToDataTable(expression);
        }

        #region Private methods

        private static bool IsBasicType(Type type)
        {
            type = Nullable.GetUnderlyingType(type) ?? type;

            return type.IsEnum || dataTypes.Contains(type);
        }

        private static DataTable GetDataTable&lt;T&gt;(this IList&lt;T&gt; data, IEnumerable&lt;PropertyDescriptor&gt; mappedProperties)
        {
            DataTable table = new DataTable();

            // columns
            foreach(PropertyDescriptor prop in mappedProperties)
            {
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }

            // row values
            foreach(T item in data)
            {
                DataRow row = table.NewRow();

                foreach(PropertyDescriptor prop in mappedProperties)
                {
                    object value = prop.GetValue(item) ?? DBNull.Value;
                    row[prop.Name] = value;
                }

                table.Rows.Add(row);
            }

            return table;
        }

        #endregion
    }
}

</pre>
<p><a style="color:#fff;margin-top:20px;" name="section4"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Using the code</span></h2>
<p>Assuming a table named <strong>dbo.Albums</strong> (I&#8217;m using <a href="http://mvcmusicstore.codeplex.com/" title="Mvc Music Store" target="_blank">Mvc Music Store</a> model):</p>
<p><a href="https://ruijarimba.files.wordpress.com/2012/03/dbo-albums.png"><img src="https://ruijarimba.files.wordpress.com/2012/03/dbo-albums.png?w=584" alt="" title="dbo.Albums"   class="aligncenter size-full wp-image-581" /></a></p>
<p><a style="color:#fff;margin-top:20px;" name="section1-1"></a></p>
<h3 style="font-size:12pt;"><strong>1. Basic usage</strong></h3>
<p>This example shows how to use a model object whose property names match the database column names from the table above:</p>
<pre class="brush: csharp; title: ; notranslate">
public class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
}
</pre>
<p>All you need to do is to specify the connection string and the destination table:</p>
<pre class="brush: csharp; title: ; notranslate">
IEnumerable&lt;Album&gt; data = GetData();

var bulkCopy = new BulkCopy() {
    ConnectionString = ConnectionString,
    DestinationTableName = &quot;dbo.Albums&quot;
};

bulkCopy.WriteToServer(data);
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-2"></a></p>
<h3 style="font-size:12pt;"><strong>2. Specifying batch size and bulk options</strong></h3>
<p>Using the model from the previous example, you can specify some other options:</p>
<pre class="brush: csharp; title: ; notranslate">
IEnumerable&lt;Album&gt; data = GetData();

var bulkCopy = new BulkCopy() {
    BatchSize = 200,
    ConnectionString = ConnectionString,
    DestinationTableName = &quot;dbo.Albums&quot;
};

// SqlBulkCopyOptions.CheckConstraints: Check constraints while data is being inserted. 
// By default, constraints are not checked.
bulkCopy.WriteToServer(data, SqlBulkCopyOptions.CheckConstraints);
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-3"></a></p>
<h3 style="font-size:12pt;"><strong>3. Using column mappings</strong></h3>
<p>This example shows how to use a model object whose property names don&#8217;t match the database column names:</p>
<pre class="brush: csharp; highlight: [3,8]; title: ; notranslate">
public class Album
{
    public virtual int Id { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string AlbumTitle { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtImage { get; set; }
}
</pre>
<p>In this case we have to create the column mappings. It can be done like this:</p>
<pre class="brush: csharp; highlight: [3,8]; title: ; notranslate">
private static IEnumerable&lt;SqlBulkCopyColumnMapping&gt; GetColumnMappings()
{
    yield return new SqlBulkCopyColumnMapping(&quot;Id&quot;, &quot;AlbumId&quot;);
    yield return new SqlBulkCopyColumnMapping(&quot;GenreId&quot;, &quot;GenreId&quot;);
    yield return new SqlBulkCopyColumnMapping(&quot;ArtistId&quot;, &quot;ArtistId&quot;);
    yield return new SqlBulkCopyColumnMapping(&quot;AlbumTitle&quot;, &quot;Title&quot;);
    yield return new SqlBulkCopyColumnMapping(&quot;Price&quot;, &quot;Price&quot;);
    yield return new SqlBulkCopyColumnMapping(&quot;AlbumArtImage&quot;, &quot;AlbumArtUrl&quot;);
}
</pre>
<p>Finally, the bulk insert can be done this way:</p>
<pre class="brush: csharp; highlight: [2,10]; title: ; notranslate">
IEnumerable&lt;Album&gt; data = GetData();
IEnumerable&lt;SqlBulkCopyColumnMapping&gt; mappings = GetColumnMappings();

var bulkCopy = new BulkCopy() {
    ConnectionString = ConnectionString,
    DestinationTableName = &quot;dbo.Albums&quot;
};

bulkCopy.WriteToServer(data,SqlBulkCopyOptions.Default, mappings);
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-4"></a></p>
<h3 style="font-size:12pt;"><strong>4. Filtering properties to be copied</strong></h3>
<p>This example shows how to filter the properties of a model object to be used in the bulk insert:</p>
<pre class="brush: csharp; highlight: [9,10]; title: ; notranslate">
public partial class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
    public virtual string P1 { get; set; }
    public virtual string P2 { get; set; }
}
</pre>
<p>Properties <strong>P1</strong> and <strong>P2</strong> don&#8217;t match any column of the table above, so they cannot be used in the bulk insert operation. Creating a filter to exclude those properties and using it can be done like this:</p>
<pre class="brush: csharp; highlight: [4,5,13]; title: ; notranslate">
// properties to exclude from mapping
var nonMappedProperties = new string[] { &quot;P1&quot;, &quot;P2&quot; };

Func&lt;PropertyDescriptor, bool&gt; expression = x =&gt; 
    !nonMappedProperties.Contains(x.Name);

IEnumerable&lt;Album&gt; data = GetData();

var bulkCopy = new BulkCopy() {
    BatchSize = 200,
    ConnectionString = ConnectionString,
    DestinationTableName = &quot;dbo.Albums&quot;,
    ExpressionFilter = expression
};

bulkCopy.WriteToServer(data, SqlBulkCopyOptions.CheckConstraints);
</pre>
<p>That&#8217;s it! In the next article I&#8217;ll show you how to integrate easily this wrapper class in Entity Framework (creating extension methods for DbContext and ObjectContext).</p>
<p><a style="color:#fff;margin-top:20px;" name="section-references"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">References</span></h2>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy%28v=vs.80%29.aspx" title="System.Data.SqlClient.SqlBulkCopy" target="_blank">System.Data.SqlClient.SqlBulkCopy</a>
<li><a href="http://msdn.microsoft.com/en-us/library/ex21zs8x%28v=vs.80%29.aspx" title="SqlBulkCopy.WriteToServer(DataTable)" target="_blank">SqlBulkCopy.WriteToServer(DataTable)</a></li>
<li><a href="http://www.4guysfromrolla.com/articles/102109-1.aspx" title="Using SqlBulkCopy To Perform Efficient Bulk SQL Operations" target="_blank">Using SqlBulkCopy To Perform Efficient Bulk SQL Operations</a></li>
<li><a href="http://stackoverflow.com/a/5805044" title="Generic List to DataTable" target="_blank">[StackOverflow] Generic List to DataTable</a></li>
</ul>
<p><a style="color:#fff;margin-top:20px;" name="section-downloads"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">Downloads</span></h2>
<p>Download the demo project (VS2010): <a title="BulkCopy-part1.zip" href="https://docs.google.com/uc?id=0B411XiA1vaSzSjhFUkJTeDNUSm1nR2tNR2E5djdkZw&amp;export=download">BulkCopy-part1.zip</a></p>
<div style="float:none;display:inline;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/C%23" rel="tag">C#</a>, <a href="http://technorati.com/tags/.NET" rel="tag">.NET</a>, <a href="http://technorati.com/tags/SQL+Server" rel="tag">SQL Server</a>, <a href="http://technorati.com/tags/ADO.NET" rel="tag">ADO.NET</a></div>
<div class="shoutIt"><a href="http://dotnetshoutout.com/Submit?url=http://wp.me/p1w7gk-9h" rev="vote-for"><br />
<img style="border:0 none;" src="http://dotnetshoutout.com/image.axd?http://wp.me/p1w7gk-9h" alt="Shout it" /></a></div>
<div style="float:none;display:inline;"><a href="http://www.dotnetkicks.com/kick/?url=http://wp.me/p1w7gk-9h"><br />
<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?http://wp.me/p1w7gk-9h&amp;bgcolor=FFCC00" alt="kick it on DotNetKicks.com" border="0" /></a></div>
<br />Filed under: <a href='http://ruijarimba.wordpress.com/category/net/'>.NET</a>, <a href='http://ruijarimba.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://ruijarimba.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://ruijarimba.wordpress.com/tag/net/'>.NET</a>, <a href='http://ruijarimba.wordpress.com/tag/ado-net/'>ADO.NET</a>, <a href='http://ruijarimba.wordpress.com/tag/bulk-insert/'>Bulk Insert</a>, <a href='http://ruijarimba.wordpress.com/tag/c/'>C#</a>, <a href='http://ruijarimba.wordpress.com/tag/sqlbulkcopy/'>SqlBulkCopy</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruijarimba.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruijarimba.wordpress.com/575/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=575&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ruijarimba.wordpress.com/2012/03/25/bulk-insert-dot-net-applications-part1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/758a1b8e56090cab54a177355b391d3d?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ruijarimba</media:title>
		</media:content>

		<media:content url="https://ruijarimba.files.wordpress.com/2012/03/bulkcopy-cs1.png" medium="image">
			<media:title type="html">BulkCopy.cs</media:title>
		</media:content>

		<media:content url="https://ruijarimba.files.wordpress.com/2012/03/dbo-albums.png" medium="image">
			<media:title type="html">dbo.Albums</media:title>
		</media:content>

		<media:content url="http://dotnetshoutout.com/image.axd?http://wp.me/p1w7gk-9h" medium="image">
			<media:title type="html">Shout it</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?http://wp.me/p1w7gk-9h&#38;bgcolor=FFCC00" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework: Get mapped table name from an entity</title>
		<link>http://ruijarimba.wordpress.com/2012/03/18/entity-framework-get-mapped-table-name-from-an-entity/</link>
		<comments>http://ruijarimba.wordpress.com/2012/03/18/entity-framework-get-mapped-table-name-from-an-entity/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 17:45:03 +0000</pubDate>
		<dc:creator>Rui Jarimba</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://ruijarimba.wordpress.com/?p=556</guid>
		<description><![CDATA[Extension methods for ObjectContext and DbContent to get the mapped table name from an entity. Table of contents The problem The solution The extension methods Using the code References The problem I am working on a set of extension methods &#8230; <a href="http://ruijarimba.wordpress.com/2012/03/18/entity-framework-get-mapped-table-name-from-an-entity/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=556&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Extension methods for ObjectContext and DbContent to get the mapped table name from an entity.</p>
<p><strong>Table of contents</strong></p>
<ul>
<li><a title="The problem" href="#section1">The problem</a>
<li><a title="The problem" href="#section2">The solution</a>
<ul>
<li><a title="The extension methods" href="#section1-1">The extension methods</a></li>
<li><a title="Using the code" href="#section1-2">Using the code</a></li>
</ul>
</li>
<li><a class="download" href="#section-references">References</a></li>
</ul>
<p><a style="color:#fff;margin-top:20px;" name="section1"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">The problem</span></h2>
<p>I am working on a set of extension methods to perform a bulk insert using Entity Framework, using internally the SqlBulkCopy object. One of the steps involved is to get the mapped table name from an entity. After some google searching, I found a <a href="http://stackoverflow.com/a/2735316" title="Get Database Table Name from Entity Framework MetaData" target="_blank">post on StackOverflow</a> that led me to the solution.</p>
<p><a style="color:#fff;margin-top:20px;" name="section2"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">The solution</span></h2>
<p>The trick is to use the method <a title="ObjectQuery.ToTraceString Method" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx" target="_blank">ObjectQuery.ToTraceString</a> to generate a SQL Select statement for an entity, and then extract the table name from that statement. </p>
<p>Let&#8217;s assume that you have an entity named <strong>Album</strong> corresponding to a table named <strong>dbo.Albums</strong>. </p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
// context is ObjectContext
string sql = context.CreateObjectSet&lt;T&gt;().ToTraceString();

...
</pre>
<p>The generated SQL for that entity can be something like this:</p>
<pre class="brush: sql; title: ; wrap-lines: false; notranslate">
SELECT 
[Extent1].[AlbumId] AS [AlbumId], 
[Extent1].[GenreId] AS [GenreId], 
[Extent1].[ArtistId] AS [ArtistId], 
[Extent1].[Title] AS [Title], 
[Extent1].[Price] AS [Price], 
[Extent1].[AlbumArtUrl] AS [AlbumArtUrl]
FROM [dbo].[Albums] AS [Extent1] 
</pre>
<p>So, all we need to do is to parse the SELECT statement to get the table name. This is the approach used in the post above but it has some limitations &#8211; that code will work only for tables that are in the default SQL Server schema (<strong>dbo.{tableName}</strong>). I made some changes to that code and I&#8217;m extracting the full table name using regular expressions.</p>
<p><a style="color:#fff;margin-top:20px;" name="section1-1"></a></p>
<h3 style="font-size:12pt;"><strong>The extension methods</strong></h3>
<p>I have created one extension method for <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext%28v=vs.103%29.aspx" title="System.Data.Entity.DbContext" target="_blank">DbContext</a> and other for <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" title="System.Data.Objects.ObjectContext" target="_blank">ObjectContext</a>:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
public static class ContextExtensions
{
    public static string GetTableName&lt;T&gt;(this DbContext context) where T : class
    {
        ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext;

        return objectContext.GetTableName&lt;T&gt;();
    }

    public static string GetTableName&lt;T&gt;(this ObjectContext context) where T : class
    {
        string sql = context.CreateObjectSet&lt;T&gt;().ToTraceString();
        Regex regex = new Regex(&quot;FROM (?&lt;table&gt;.*) AS&quot;);
        Match match = regex.Match(sql);

        string table = match.Groups[&quot;table&quot;].Value;
        return table;
    }
}
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section1-2"></a></p>
<h3 style="font-size:12pt;"><strong>Using the code</strong></h3>
<p>Getting the mapped table name for an entity named <strong>Album</strong>, using a <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" title="System.Data.Objects.ObjectContext" target="_blank">ObjectContext</a> object:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
ObjectContext context = ....;
string table = context.GetTableName&lt;Album&gt;();
</pre>
<p>Or using a <a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext%28v=vs.103%29.aspx" title="System.Data.Entity.DbContext" target="_blank">DbContext</a> object:</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">
DbContext context = ....;
string table = context.GetTableName&lt;Album&gt;();
</pre>
<p><a style="color:#fff;margin-top:20px;" name="section-references"></a></p>
<h2 style="border-bottom:1px solid #3366ff;font-size:14pt;"><span style="color:#3366ff;">References</span></h2>
<ul>
<li><a title="Get Database Table Name from Entity Framework MetaData" href="http://stackoverflow.com/a/2735316" target="_blank">[StackOverflow] Get Database Table Name from Entity Framework MetaData</a></li>
<li><a title="ObjectQuery.ToTraceString Method" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx" target="_blank">[MSDN] ObjectQuery.ToTraceString Method</a></li>
</ul>
<div style="float:none;display:inline;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/.NET" rel="tag">.NET</a>, <a href="http://technorati.com/tags/Entity+Framework" rel="tag">Entity Framework</a></div>
<div class="shoutIt"><a href="http://dotnetshoutout.com/Submit?url=http://wp.me/p1w7gk-8Y" rev="vote-for"><br />
<img style="border:0 none;" src="http://dotnetshoutout.com/image.axd?http://wp.me/p1w7gk-8Y" alt="Shout it" /></a></div>
<div style="float:none;display:inline;"><a href="http://www.dotnetkicks.com/kick/?url=http://wp.me/p1w7gk-8Y"><br />
<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?http://wp.me/p1w7gk-8Y&amp;bgcolor=FFCC00" alt="kick it on DotNetKicks.com" border="0" /></a></div>
<br />Filed under: <a href='http://ruijarimba.wordpress.com/category/net/'>.NET</a>, <a href='http://ruijarimba.wordpress.com/category/codeproject/'>CodeProject</a>, <a href='http://ruijarimba.wordpress.com/category/entity-framework/'>Entity Framework</a> Tagged: <a href='http://ruijarimba.wordpress.com/tag/c/'>C#</a>, <a href='http://ruijarimba.wordpress.com/tag/entity-framework/'>Entity Framework</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ruijarimba.wordpress.com/556/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ruijarimba.wordpress.com/556/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ruijarimba.wordpress.com&#038;blog=22430752&#038;post=556&#038;subd=ruijarimba&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ruijarimba.wordpress.com/2012/03/18/entity-framework-get-mapped-table-name-from-an-entity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/758a1b8e56090cab54a177355b391d3d?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ruijarimba</media:title>
		</media:content>

		<media:content url="http://dotnetshoutout.com/image.axd?http://wp.me/p1w7gk-8Y" medium="image">
			<media:title type="html">Shout it</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?http://wp.me/p1w7gk-8Y&#38;bgcolor=FFCC00" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>
