ZZZ Code AI
Report Issues
Suggest Features
Login
Dapper Tools
Chat
Code Explain
Seed Data Generator
Sql Generator
Stored Procedure Generator
Entity to Table Converter
Table to Entity Converter
Sql Injection Detector
More Tools
How to use a stored procedure to retrieve a returned specific column in Dapper?
<p>To use a stored procedure and retrieve a returned specific column, you have 3 choices:</p> <ol> <li>By mapping returned data to an anonymous type and select your column:</li> </ol> <pre><code class="language-csharp">using (var connection = new SqlConnection("connectionString")) { var result = connection.Query("MyStoredProcedure", commandType: CommandType.StoredProcedure).ToList(); var specificColumnList = result.Select(x => (string)x.MyColumn).ToList(); } </code></pre> <ol start="2"> <li>By mapping returned data to a strongly typed object and select your column:</li> </ol> <pre><code class="language-csharp">public class MyStoredProcedureResult { public string MyColumn { get; set; } // Add other properties as needed, to match all columns returned by the stored procedure } using (var connection = new SqlConnection("connectionString")) { var result = connection.Query<MyStoredProcedureResult>("MyStoredProcedure", commandType: CommandType.StoredProcedure).ToList(); var specificColumnList = result.Select(x => x.MyColumn).ToList(); } </code></pre> <ol start="3"> <li>By using <a href="https://www.learndapper.com/dapper-query/selecting-multiple-results">QueryMultiple (Multi-Mapping)</a> and read returned data with an anonymous type or strongly typed object:</li> </ol> <pre><code class="language-csharp">using (var connection = new SqlConnection("connectionString")) { using (var multi = connection.QueryMultiple("MyStoredProcedure", commandType: CommandType.StoredProcedure)) { var firstResultSet = multi.Read().ToList(); var specificColumnFromFirstResultSet = firstResultSet.Select(x => (string)x.MyColumn).ToList(); } } </code></pre> <div class="alert alert-warning"><p>NOTE: All columns are returned from the stored procedure, not only the desired selected column.</p> </div>
Sponsored by
Dapper Plus
This field is required
A text with a minimum of 10 characters is required
Send
Legal & Licensing
Answer generated by AI may produce inaccurate information about code, people, facts, and more.
Advertising Break!
5
seconds left
Did you know...
That you can now sponsor this project on
GitHub
?