I have a function that is trying to return a list of all transactions from the database. To do this it needs to retrieve all the data from TransactionItems, and then join that to the data retrieved from Transactions. I want to be able to do this without creating any new classes (if at all possible)
public async Task<IEnumerable<object>> GetTransactionBySaleId(int id)
{
var query = ("SELECT t.*, ti.* " +
"FROM Transactions t " +
"INNER JOIN TransactionItems ti ON t.SaleId = ti.SaleId " +
"WHERE t.SaleId = @Id");
// Parameterisation to prevent injection attacks
var idParam = new SqlParameter("Id", id);
return await DatabaseConnection.Instance.ExecuteQueryAsync<object>(query, idParam);
}
My ExecuteQueryAsync method:
public async Task<IEnumerable<T>> ExecuteQueryAsync<T>(string query, params SqlParameter[] parameters) where T : new()
{
using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
var resultList = new List<T>();
using var command = new SqlCommand(query, connection);
// Add parameters to the command if any
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var obj = new T();
MapData(reader, obj);
resultList.Add(obj);
}
return resultList;
}
My issue is that the return type is currently object, and all this does is return empty objects for each entry in the tables. I first had it set to return Transactions but this didn't display the data from TransactionItems.
Data in my Transactions table: Transactions
Data in my TransactionItems tale: TransactionItems
I'm still relatively inexperienced, so any help would be appreciated