× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Creating list of Objects from Database

Michael Perrenoud
Feb 06, 2015
<p>So if you want to get a list of objects I'd submit to you that you want to use Dapper. You can get Dapper in your project via NuGet. Once you've done that you need to build a class that mimics your <code>SELECT</code> statement. For the sake of argument let's say your <code>SELECT</code> statement is:</p> <pre><code>SELECT * FROM TagsTable </code></pre> <p>and let's say <code>TagsTable</code> looks like this:</p> <pre><code>public class TagsTable { public int Id { get; set; } public string Tag { get; set; } } </code></pre> <p>(remember this is just to show you how it works)</p> <p>Now your code looks like this:</p> <pre><code>private void LoadList() { List&lt;TagsTable&gt; tagsList; using (IDbConnection connection = new SqlConnection(Properties.Settings.Default.DBConnectionString)) { tagsList = connection.Query&lt;TagsTable&gt;(sqlStatement); } } </code></pre> <p>Here the <code>Query</code> method is an extension method that hangs off of the <code>IDbConnection</code>; that's how Dapper works. Not only is the code concise, and the mapping done for you, but Dapper is <em>fast.</em></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/23807032/Creating%20list%20of%20Objects%20from%20Database/23807208">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus