Ah Sa Fa
Ah Sa Fa

Reputation: 181

Dapper: How to get value of query result in Entity Framework Core

I am using Dapper to get column value from database in Entity Framework Core like this:

var query = await DbConnection.QueryAsync("SELECT AttachmentId FROM dbo.SystemSettings ");

var attachment = query.FirstOrDefault();

This code returns

{{ DapperRow, AttachmentId = '10' }}

How could I get the value of AttachmentId which is 10 to use it in the next query

DbConnection.QueryAsync<Attachment>("SELECT * FROM dbo.Attachment WHERE Id = @Id", ???);

Upvotes: 0

Views: 1328

Answers (2)

Ah Sa Fa
Ah Sa Fa

Reputation: 181

Thanks @Fabio for your answer :

SELECT *
FROM dbo.Attachment 
WHERE Id IN (SELECT AttachmentId FROM dbo.SystemSettings)

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062965

to keep with the 2-query theme in the question:

Int id = query.AttachmentId;
var objs = DbConnection.QueryAsync<Attachment>("SELECT * FROM dbo.Attachment WHERE Id = @Id", new { id });

Note that when reading a single column, it may be preferable to use QueryAsync<int> or similar, rather than the dynamic API.

Upvotes: 3

Related Questions