无法隐式转换类型'System.Collections.Generic.IEnumerable To System.Collections.Generic.IEnumerable <tiendadeportes.producto> </tiendadeportes.producto>

时间:2014-01-25 06:33:25

标签: c# asp.net

我有一个属性

public IEnumerable<producto> p_producto { 
    get { 
        return ((from ii in contexto.productos
                 orderby ii.nombre ascending
                 select new producto {
                               productoID = ii.productoId, 
                               nombre = ii.nombre,
                               descripcion = ii.descripcion, 
                               categoria = ii.categoria,
                               precio = ii.precio}));
        }
}

在Default.aspx

public IEnumerable<producto> filtroCategoria()
{
    IEnumerable<producto> productos = repositorio.p_producto; <---- Error why ?

}

摘要

  

无法隐式转换类型   'System.Collections.Generic.IEnumerable<TiendaDeportes.Modelos.producto>'   到'System.Collections.Generic.IEnumerable<TiendaDeportes.producto>'。   存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:2)

您有两个名为producto的班级。一个在TiendaDeportes命名空间内,另一个在TiendaDeported.Modelos中。您的存储库返回第一个存储库中的IEnumerable<T>,并且您在Default.aspx中的属性返回第二个存储库中的IEnumerable<T>。它无法运作。

要么在Default.aspx内更改您的属性以返回IEnumerable<TiendaDeported.Modelos.producto>,要么进行额外的Select调用,以便将TiendaDeported.Modelos.producto映射到TiendaDeported.producto