tickwave
tickwave

Reputation: 3455

cannot convert from System.Collections.Generic.List to System.Collections.Generic.IEnumerable

Any idea why am I getting this error? I thought List implements IEnumerable.

        var customization = new List<CustomerOrderCustomizationDTO>();

        customization.Add(new CustomerOrderCustomizationDTO()
        {
            ProductCustomizationID = _uow.Product.GetCustomization("LENGTH").ID,
            Value = length.ToString()
        });

        customization.Add(new CustomerOrderCustomizationDTO()
        {
            ProductCustomizationID = _uow.Product.GetCustomization("WIDTH").ID,
            Value = width.ToString()
        });

        customization.Add(new CustomerOrderCustomizationDTO()
        {
            ProductCustomizationID = _uow.Product.GetCustomization("WEIGHT").ID,
            Value = weight.ToString()
        });

        return _uow.Product.GetProductPrice(productID, ref customization); //ERROR

Interface

decimal GetProductPrice(int productID, ref IEnumerable<CustomerOrderCustomizationDTO> custOrderCustomizations);

Upvotes: 1

Views: 4815

Answers (2)

Jedi_Maseter_Sam
Jedi_Maseter_Sam

Reputation: 813

When you use ref it is sort of like a pointer in c++. That being said, the types have to match, not be heritable. You will need to cast customization to IEnumerable<CustomerOrderCustomizationDTO> in order to pass it by ref. You can read more about the ref keyword here.

You might be able to get away with removing ref since List<> is a reference type and is not passed by value like int for example. Then you would not have to cast.

Upvotes: 1

Jack A.
Jack A.

Reputation: 4453

Because custOrderCustomizations is a ref parameter, that means that the parameter type (IEnumerable) must be assignable to the type of the variable that you pass in. In this case, you are passing in the customization variable, which is a List. You can't assign an IEnumerable to a List.

One solution would be to assign your customization variable to a new variable of type IEnumerable and pass that to GetProductPrice, like so:

IEnumerable<CustomerOrderCustomizationDTO> tempCustomizations = customization;
return _uow.Product.GetProductPrice(productID, ref tempCustomizations);

Upvotes: 3

Related Questions