I've been trying to create methods where I use dapper's QueryMultiple. I've managed to get the multiple query to work, and when I debug I can see that the values from the database gets returned. However, I would like to see the data in Postman when calling the API. Here's where the problems start, since I can't seem to get any return type to work, it's almost always the same error. The error is almost always regarding "Cannot convert". Examples of errors are:
Error CS1503 Argument 1: cannot convert from 'string' to \RequestRepo.cs 45 Active
Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.IEnumerable<V4ReactAPI.Models.V4RequestSite.ViewModels.RequestOverview>'. An explicit conversion exists (are you missing a cast?) 90 Active
The controller looks like:
[HttpGet]
public async Task<IActionResult> testa(RequestOverview model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
await _request.testa(model);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
The model:
public class VehicleReg
{
public string vehicleRegNr { get; set; } = "";
}
public class TestIgen
{
public string ownerName { get; set; } = "";
}
(Name of the whole model class is RequestOverview
)
Now the method, which i have in an own class:
public async Task<IEnumerable<RequestOverview>> testa(RequestOverview model)
{
string query = "SELECT TOP 5 RegNumber as vehicleRegNr FROM [dbo].[Vehicle];
SELECT TOP 5 Name as ownerName FROM [dbo].[Customer];";
var result = _sqlconnection.QueryMultiple(query);
var vehicles = result.Read<VehicleReg>();
var test = result.Read<TestIgen>();
List<string> allQueries = new List<string>();
foreach (var item in vehicles)
{
allQueries.Add(item.vehicleRegNr);
}
foreach (var item in test)
{
allQueries.Add(item.ownerName);
return allQueries.ToList();
}
This throws an ERROR directly (The CS0266
as shown before).
Basically, I need to return the var vehicles
and var test
returned in some way, to be able to get the data to be shown when calling the api in postman for example.
I've also tried this method, but again I'm having trouble returning the data:
public async Task<bool> Test()
{
string query = "SELECT TOP 5 RegNumber as vehicleRegNr FROM [dbo].[Vehicle];
SELECT TOP 5 Name as ownerName FROM [dbo].[Customer];";
var result = _sqlconnection.QueryMultiple(query);
using (var multi = _sqlconnection.QueryMultiple(query, null))
{
List<VehicleReg> list1 = multi.Read<VehicleReg>().AsList();
List<TestIgen> list2 = multi.Read<TestIgen>().AsList();
}
return true;
}
Anyone have any ideas on how to return the data?
[Update] I have tried the following to avoid the converting error, but it's still not returning any data when I try it in Postman:
public async Task<List<string>> Test()
{
string query = "SELECT TOP 5 RegNumber as vehicleRegNr FROM [dbo].[Vehicle];
SELECT TOP 5 Name as ownerName FROM [dbo].[Customer];";
List<string> allQueries = new List<string>();
using (var multi = _sqlconnection.QueryMultiple(query, null))
{
//List<string> allQueries = new List<string>();
List<VehicleReg> list1 = multi.Read<VehicleReg>().AsList();
List<TestIgen> list2 = multi.Read<TestIgen>().AsList();
//return list1.ToList();
allQueries.Add(list1.ToString());
allQueries.Add(list2.ToString());
}
return allQueries;
}