✅ (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
yeahhh this is exactly what i'm tryna do ^
let me see
so in the end, how can i output the first result?
i'm sorry if i'm being unclear but this is my first time using dapper lol
result
is just Task<IEnumerable<Request>>
right now.yea
Since you're in a method that can be marked as
async
, do that, and await
before you get the result.
That'll turn result
into IEnumerable<Request>
.
Then you can return result?.SingleOrDefault()
yeah, but is there any way to return it as a task? thinking again, i probably dont wanna
await
in the function
where i wanna await tho is in function calls, e.g when retreiving the requests
Correct?
You want to await the full invocation?
yep exactly
You're still fine
1 sec
I'll set up an example right here in eval so you can see it in action
it won't create unnecessary blocking right?
Inside of
GetRequest
you have to wait on the result to come back at some point.
Considering there's no work between the execution of the query and when you return the result in GetRequest
there's no reason to do it the hard way, just use await
.
If you're worried about blocking the UI, then append .ConfigureAwait(false)
to the end of your query async invocation.
From a consumption of GetRequest
perspective
If you want to start the query but have other work to do, you assign the task to start it and await it when you need it
The difference:
An eval might helpyeah i think i'll figure that on my own
i feel like im wasting your time
thanks a lot for your answers
Hazel | へいぜる
REPL Result: Success
Console Output
Compile: 639.555ms | Execution: 109.927ms | React with ❌ to remove this embed.
Not wasting my time, I'm just listening in on a meeting 🙂 hopefully that eval helps a bit
Actually, a better example
1 sec
well as long as the inside of
GetRequest(ID: 1)
doesn't block the program because of that await DBHandler.connection.QueryAsync
it's good. if it blocks when calling it (await GetRequest(ID: 1)
) it doesn't matter because i'm calling the function in another thread anywayHazel | へいぜる
REPL Result: Success
Console Output
Compile: 662.170ms | Execution: 6140.932ms | React with ❌ to remove this embed.
Notice the difference in timestamps here:
Versus here:
You can see that we had to wait on
foo
to notify the consumer we were wating on it, versus with bar
we started the task, then notified, and when it finished it printed the result because we needed bar
to continue.
So you're more concerned with blocking the UI?
Then do:
yeah done
well this is pretty much the API of the program, i'll have other scripts (cs-script) which will poll this function
You'll have to employ similar strategies where you consume the API to prevent blocking.
yeah
i appreciate your help
No problem 🙂
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.