For every ProductSpecificationAttribute in OrderItems

8 months ago
Hello!  What if I need to iterate throught every ProductSpecificationAttribute in an order?  The following code fails because OrderItem does not have GetEnumerator.


foreach (OrderItem item in order.OrderItems)
            {
                foreach (ProductSpecificationAttribute spec in item)
                {

                }
            }


Thanks!

Jeremy
8 months ago
Hi Jeremy,

You can iterate through an enumerator but, order item isn't enumerator. Besides that, ProductSpecificationAttribute collection is a member of the product entity class, not order item. So, your code should look like this:

foreach (OrderItem item in order.OrderItems)
{
    foreach (ProductSpecificationAttribute spec in item.Product.ProductSpecificationAttributes)
    {

    }
}
8 months ago
resanehlab wrote:
Hi Jeremy,

You can iterate through an enumerator but, order item isn't enumerator. Besides that, ProductSpecificationAttribute collection is a member of the product entity class, not order item. So, your code should look like this:

foreach (OrderItem item in order.OrderItems)
{
    foreach (ProductSpecificationAttribute spec in item.Product.ProductSpecificationAttributes)
    {

    }
}


resanehlab, that works perfectly.  Thanks for the clarity.

Have an excellent day!

Jeremy