4

I have a result set that might look like this:

ID    (no column name)  anotherID
----  ----------------  ----------
1        super            3
1        super            4
3        duper            6
4        really           7
4        really           8

I have 2 issues:

First: How do I use dapper with a column with no name?

Second: I want to have a parent child relationship such that I get 3 objects each with a list of anotherID's for example:

public class MyObject
{
   public int ID
   public string Name
   public int[] Children
}
John Sobolewski
  • 4,512
  • 1
  • 20
  • 26

1 Answers1

4

Well, un-named columns are not supported by dapper. I never really saw a reason for them.

I guess we could build support for:

class Foo { [ColumnNumber(1)] public string Name {get;set;} }

The trouble is that it introduces a very fragile method of querying which I strongly dislike, passing a directive to Query is just as clunky.

However, if you are happy to change the way you grab the results you could work around this.

var grid = QueryMultiple(@"set nocount on 
declare @t table(Id int, Name nvarchar(max), AnotherId int)

insert @t
exec proc

set nocount off 
select Id, Name from @t
select Id, AnotherId from @t
");

Then use the technique here to multi map: Multi-Mapper to create object hierarchy

Community
  • 1
  • 1
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • 1
    perhaps a cleaner way to do this would be to pass in a mapping... to essentially give names to the columns... so that you would specify it as an extra parameter to the query so you don't muddy up the data object definition. Especially if you reuse the data object for multiple queries. Alternatively you could also allow for remapping of names to other names... IE... underlying data (stored proc) returns a bunch of Hungarian notation you don't want in your data objects. – John Sobolewski Sep 07 '11 at 12:13
  • I'm in the same boat as jsobo, I have a stored proc that I have no control over.. but need to use the stored proc with multiple unnamed columns for a reporting application. I've tried to adjust the query, but I get the SQL error "An INSERT EXEC statement cannot be nested." .Ideally if I could refer to column numbers that would solve my problem. How can I modify drapper to add support for: class Foo { [ColumnNumber(1)] public string Name {get;set;} } – studiothat Jun 07 '12 at 05:29