-3

How can I optimize the following piece of code?

IList<OrderItem> OrderItemsList = new List<OrderItem>();
while (orderItemsResult.Read())
{
    new OrderItem()
    {
        ItemName = orderItemsResult.GetString("item_name"),
        Price = orderItemsResult.GetFloat("price"),
        Quantity = orderItemsResult.GetInt32("quantity")
    },
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
scikid
  • 7
  • 4
  • could you be more specific - whats wrong & what you want to do? – murksiuke Mar 16 '19 at 11:21
  • 2
    I guess you want to `Add()` the new `OrderItem` to `OrderItemsList`? – Crowcoder Mar 16 '19 at 11:22
  • Am trying to populate the DB results to the object initialization array but it returned 0 results. I have already checked the DB query and it is fine. I need to populate these objects for Runtime Text Template. – scikid Mar 16 '19 at 11:31
  • 1
    Use this inside `while` => `OrderItemsList.Add(new OrderItem { ItemName = orderItemsResult.GetString("item_name"), Price = orderItemsResult.GetFloat("price"), Quantity = orderItemsResult.GetInt32("quantity") });` – er-sho Mar 16 '19 at 11:32
  • 1
    Thanks @er-sho haha! Am such a noob. – scikid Mar 16 '19 at 11:36

3 Answers3

1

Although a bit late to reply ,still I shall add my thoughts. We can do without List<OrderItem> object. The below code returns IEnumerable<OrderItem>

while (orderItemsResult.Read())
{
   yield return new OrderItem()
   {
        ItemName = orderItemsResult.GetString("item_name"),
        Price = orderItemsResult.GetFloat("price"),
        Quantity = orderItemsResult.GetInt32("quantity")
    };
}
Ajeet Kumar
  • 687
  • 6
  • 12
  • Thanks Ajeet! Much appreciated. I never thought there are numerous ways of doing this. Great help for a week old c# developer like me. – scikid Mar 17 '19 at 14:05
0

As @crowcoder and @er-sho pointed out in the comments, you need to add the OrderItem to OrderItemsList:

IList<OrderItem> OrderItemsList = new List<OrderItem>();
while (orderItemsResult.Read())
{
    var orderItem = new OrderItem()
    {
        ItemName = orderItemsResult.GetString("item_name"),
        Price = orderItemsResult.GetFloat("price"),
        Quantity = orderItemsResult.GetInt32("quantity")
    };

    // add to the list
    OrderItemsList.Add(orderItem);
}
haldo
  • 14,512
  • 5
  • 46
  • 52
-1

As in the accepted answer, you can implement it as an iterator function, but keep in mind that in case orderedItemsResult is an IDisposable such as IDataReader for reading from something like a database (the methods called on it appear that this is the case) to make sure to embed it in a try/finally or using block. For example:

IEnumerable<OrderItem> GetOrders(string connectionString, string queryString)
{
    IDbConnection connection = new MyDbConnection(connectionString);
    IDbCommand command = null;
    IDbReader orderedItemsResult = null;
    try
    {
        connection.Open();
        command = new MyCommand(queryString, connection);
        orderedItemsResult = command.ExecuteReader();
        while (orderItemsResult.Read())
        {
            yield return new OrderItem()
            {
                ItemName = orderItemsResult.GetString("item_name"),
                Price = orderItemsResult.GetFloat("price"),
                Quantity = orderItemsResult.GetInt32("quantity")
            };
        }
    }
    finally
    {
        orderItemsResult?.Dispose();
        command?.Dispose();
        connection.Dispose();
    }
}

The IEnumerable returned which implements IDisposable as well executes the finally block whenever it is disposed (for example, when a foreach is done with it)