0

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

9
  • Why not use a SqlDataReader? - learn.microsoft.com/en-us/dotnet/api/…
    – bdcoder
    Commented Mar 14 at 15:35
  • I cant use SqlDataReader because the number of rows will be different for both tables
    – Luke 135
    Commented Mar 14 at 17:00
  • Not sure I understand how that would prevent the use of SqlDataReader, if you can provide some sample data or expand on your example that may help. Thanks.
    – bdcoder
    Commented Mar 14 at 20:01
  • I have added images of the data in my tables. Like I said, if i change object to Transaction on the line DatabaseConnection.Instance.ExecuteQueryAsync<object> then it only returns SaleId, CustomerId, DateAndTime and TotalCost. Hope this makes sense
    – Luke 135
    Commented Mar 15 at 12:35
  • Can you refactor your code to make use of a SqlDataReader rather than an Task<IEnumerable<object>>, i.e.: Task<SqlDataReader> - essentially, the SqlDataReader would allow you to iterate over the result set and would give you access to all columns in the result of your SQL join statement.
    – bdcoder
    Commented Mar 15 at 15:20

0

Browse other questions tagged or ask your own question.