简体   繁体   中英

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", ???);

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.

Thanks @Fabio for your answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM