✅ (Solved) (Dapper) how to make a single query with one-to-many objects?
i don't think
QuerySingleAsync
(or QuerySingleOrDefaultAsync
) accepts more than 1 template object, or am i missing something here?36 Replies
You can use mapping
i'm already doing so?
Stack Overflow
Custom mapping in Dapper
I'm attempting to use a CTE with Dapper and multi-mapping to get paged results. I'm hitting an inconvenience with duplicate columns; the CTE is preventing me from having to Name columns for example...
yeah i am, problem is it only works (i think) with multiple rows
You're saying you already have a mapping in place and you just want a single object back?
Like a single record
yeah pretty much
To my knowledge, it doesn't exist.
You'd have to manually grab the single
You could always create your own extension for
MySqlConnection
though if you want to hide that awaycan i get around it? like doing a normal query but returning the first element
i'm having trouble with async/ienumerable, i can't get a function to return [0]
It's just returning
Task<IEnumerable<Request>>
, so
1 secis
connection.Query
a blocking function?Yeah
But you can get around that
ah so it's fine then, i don't use tasks properly i always await
i'm working on a multi-threaded application so it doesn't matter
I'd just do something like:
Personally
If you don't need the record right away
Otherwise you have to await upfront.
yeah i do, hence i await every db query
so if it's a blocking function and i don't need to map it (.then) i'm cool with it
but let me try your snippet
Well, if you need the record up front, do this instead:
The first snippet I sent is great for when you have some work you can do before you absolutely need the result.
You can also wrap a blocking call to consume it asynchronously by just creating a method that returns a task:
Something like that
Then await that instead
If you find the async versions aren't working for your needs but the sync versions do