Reputation: 10005
What's the best way to do the following...
conn.Query<Foo>(@"SELECT 1, 2, 'ZEBRA'");
Say Foo
has a constructor like so...
public Foo(Bar test, string zebra)
..And Bar
has a constructor like this:
public Bar(int one, int two)
This doesn't work, what would be the best approach to achieve the desired result.
Upvotes: 1
Views: 414
Reputation: 3231
Don't try to hydrate into your real models directly from Dapper if they have constructor requirements like that. Instead, hydrate into a private/internal class, and then instantiate and return the proper models.
internal class FooBarQueryModel
{
public string Zebra { get; set; }
public int One { get; set; }
public int Two { get; set; }
}
conn.Query<FooBarQueryModel>(sql).Select(qm => new Foo(new Bar(qm.One, qm.Two), qm.Zebra));
Upvotes: 2
Reputation: 12226
You could try non-generic Query API (more details are here https://stackoverflow.com/a/8995135/229949):
conn.Query(@"SELECT 1 as one, 2 as two, 'ZEBRA' as zebra")
.Select(_ => new Foo(new Bar(_.one, _.two), _.zebra);
Upvotes: 2