32

it looks like there was an ExecuteScalar in Dapper...

http://code.google.com/p/dapper-dot-net/issues/attachmentText?id=22&aid=220000000&name=ExecuteScalar.cs&token=9e2fd8899022f507b140ffb883c60e34

Was ExecuteScalar renamed or removed?

Can this now be achieved with .Query or .Query<T>?

sgtz
  • 8,849
  • 9
  • 51
  • 91
  • 8
    The reason that we never added one is simply: `.Single()` does the same thing. We investigated the underlying implementations etc, and there is no real benefit in adding an extra method. – Marc Gravell Nov 08 '11 at 13:20
  • .Single() is not comming up as a Dapper extension method. Is this in an underlying method in the .net framework? – sgtz Nov 08 '11 at 13:26
  • 2
    I mean: `int value = conn.Query(sql, args).Single();`, or `string value = conn.Query(sql, args).Single();`, etc – Marc Gravell Nov 08 '11 at 13:27
  • note, that is an attachment you linked to ... this never existed in core – Sam Saffron Nov 11 '11 at 01:25
  • @Sam Saffron: I appreciate that. I was a looking at it from a C# interface angle first + was adapting old code. Marc's answer about using LINQ's .Single() is far more elegant. A simple touch like this makes the framework a lot simpler. FYI: I just replaced some home grown commanding objects with Dapper. We didn't have sql "command" caching and leaned on cached reflection properties for parameters + pushed these in via tokenised sql slabs via StringBuilder. It worked okay, but Dapper is more concise and easier to use. It feels more solid. Thanks SO. – sgtz Nov 11 '11 at 06:11
  • @Sam Saffron: ... also, didn't realise that this was never a part of the core. Was getting aquanted with the open source project as I posted the question. – sgtz Nov 11 '11 at 06:20

3 Answers3

24

ExecuteScalar was just added in 1.28: https://www.nuget.org/packages/Dapper

Evan M
  • 2,573
  • 1
  • 31
  • 36
  • I got a little confused by this, if I understand this correctly, in the current version of Dapper (1.42 as on 8th Oct 2015) there is NO execute scalar. The functionality is achieved via the LINQ method Single(). Right? – Sudhanshu Mishra Oct 08 '15 at 05:35
  • 3
    1.42 still has it. It uses the underlying SqlCommand.ExecuteScalar(), which has different behavior than .Single() and .First() (notably, it will return null if no records come back). See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx – Evan M Oct 08 '15 at 20:52
12

I was able to call ExecuteScalar< T > with version 1.42.0

    public Boolean BeforeToday(DateTime dateInQuestion)
    {
        try
        {
            using (var conn = new SqlConnection(ConnectionString))
            {
                String sql = @"SELECT CONVERT(bit, CASE WHEN getdate() > @dateParameter THEN 1 ELSE 0 END) AS BeforeToday";

                var result = conn.ExecuteScalar<Boolean>(sql, new { dateParameter = dateInQuestion });

                return result;
            }
        }
        catch (Exception)
        {
            return dateInQuestion < DateTime.Now;
        }
    }
Doug Dekker
  • 353
  • 2
  • 9
4

In version 1.50.4 I was able to call connection.QuerySingle<int>(query,params)