15

I need return a list from dapper with the new tuple in C# 7.

public static List<(int StyleId, decimal StyleCode)> GetOnlyServices()
{
    var query = $@" SELECT
                    ST.style_id as StyleId, ST.style_code as StyleCode
                    ...
                    ...";

    using (SqlConnection db = new SqlConnection(InfobaseConString))
    {
        var a = db.Query<(int StyleId, decimal StyleCode)>(query, commandTimeout: 90).ToList();
        return a;
    }
}

But this function only return me 56 rows with (0,0), Item1=0, Item2=0. What I missing?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Yuri Morales
  • 2,351
  • 4
  • 25
  • 52
  • What results were you expecting? – Kirk Woll Jan 12 '17 at 16:14
  • values... i get only 0,0 in all 56 rows... I expect values... The query is correct. If I change var a = db.Query(query, commandTimeout: 90); I get correct data [2] = {{DapperRow, StyleId = '534', StyleCode = '10000005'}} – Yuri Morales Jan 12 '17 at 16:17
  • 3
    Since that's the case, it wouldn't surprise me if Dapper doesn't support this feature yet. You may have better luck posting an issue on the [project's site](https://github.com/StackExchange/dapper-dot-net). – Kirk Woll Jan 12 '17 at 16:24
  • 1
    I'd be very surprised if Dapper already supported C#7 features. I know Marc and Nick are on the ball, but I doubt they're worrying about this right now. – DavidG Jan 12 '17 at 16:25
  • maybe try db.Query>(query, commandTimeout: 90) – Bruno Jan 12 '17 at 17:34
  • 1
    or have a look at [this answer](http://stackoverflow.com/a/34883374/215553), maybe you can use that `Query` signature with the `Tuple.Create` parameter – Bruno Jan 12 '17 at 17:58

2 Answers2

15

Looks like they added this a few months ago. Here's an example usage from the tests:

    [Fact]
    public void TupleReturnValue_Works_NamesIgnored()
    {
        var val = connection.QuerySingle<(int id, string name)>("select 42 as [Item2], 'Fred' as [Item1]");
        Assert.Equal(42, val.id);
        Assert.Equal("Fred", val.name);
    }
w5l
  • 5,341
  • 1
  • 25
  • 43
John Tseng
  • 6,262
  • 2
  • 27
  • 35
  • 1
    It's disappointing that Dapper's tuple mapping is based on positions rather than names, but at least it's something. – MarredCheese Jul 09 '20 at 14:39
12

Support has been added in version 1.50.4. You should be able to access the fields using their friendlier names now.


Original Answer:

Frankly, it's just not supported the way the tuples work. Dapper's deserializer maps column values with constructor parameters or properties/fields by name in the object's type. (Source if you can understand the generated IL).

ValueTuples on the other hand still only have property names corresponding to items in the tuple (Item1, Item2, etc.) but uses compiler/ide magic to make them accessible by other names. So StyleId or StyleCode will not be actual property names, they're just aliases for Item1 and Item2 respectively.

You'll either have to wait for the team to add in explicit support for ValueTuples or use the expected property names in your query.

var query = $@" SELECT
                ST.style_id as Item1, ST.style_code as Item2
                ...
                ...";
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272