IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. Program Language
  4. C#
  5. Getting data from Dapper result

Getting data from Dapper result

2022-08-12 193 Views 0 Like 0 Comments

Dapper query result returns DapperRow collections: IEnumerable<dynamic>{List<Dapper.SqlMapper.DapperRow>}. In order to use the data inside DapperRow object, we need to convert the data into usable types.
dapper data

Table of Contents

  • Get single value
  • Get data from a single row
  • Get data as a Array:
  • Get data as a List:
  • Get data as a Dictionary:
  • Get data as an object:

Get single value

var data = conn.ExecuteScalar("select top 1 title from book");
string title = $"{data}";
int id = conn.ExecuteScalar<int>("select top 1 id from book");

Get data from a single row

var row = conn.QuerySingle("select top 1 title, url_target from book");
var title = $"{row.title}";

Get data as a Array:

var data = conn.Query("select title, url from book").Select(x => x.title).ToArray();
foreach(var d in data){
    string title = d;
}

Get data as a List:

var data = conn.Query("select title, url from book").Select(x => x.title).ToList();
foreach(var d in data){
    string title = d;
}

Get data as a Dictionary:

var data = conn.Query("select title, url from book").ToDictionary(row => (string)row.title, row => (string)row.url);
foreach(var k in data.Keys){
    string key = k;
    string value = data[k];
}

Get data as an object:

var books = conn.Query<Book>("select * from book");  //returns IEnumerable<Book>
foreach(var book in books){
    string title = book.Title;
}

Dapper automatically mapped the column data to the fields in the object by their names.

Loading

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: Blog
Last updated:2022-08-16

IT Team

This person is lazy and left nothing

Like
< Previous

Comments

Cancel reply
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
The event Calendar monthly view tooltip location Preparing SQL script to check object existance for production deployment Creating social links shortcode Remote connection to MySQL on SiteGround String concatenation in c# and php JQuery set selected option for supporting multi-language
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com