Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to FirstOrDefault() with Dapper

Tags:

dapper

I am having problems returning a single object from a Dapper query. I have a simple query and I want to get the first object from the returned collection. What am I missing?

using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
{
    string query = @"select * from order";
    sqlConnection.Open();

    // Works
    var orders = sqlConnection.Query<OrderModel>(query);

    // Does Not Work ERROR: "does not contain a definition for 'FirstOrDefault' and 
    // no extension method 'FirstOrDefault' 
    // accepting a first argument of type 'System.Collections.Generic.IEnumerable<OrderModel>'"
    var order = sqlConnection.Query<OrderModel>(query).FirstOrDefault();                                                                        
}
like image 640
Rip Avatar asked Dec 26 '22 04:12

Rip


1 Answers

You are getting this error because you have not added the namespace

using System.Linq

in your cs file.

like image 152
Krishnraj Rana Avatar answered Jan 09 '23 21:01

Krishnraj Rana