不能隐式转换类型。存在明确的转换(您是否缺少演员?) [英] Cannot implicitly convert type. An explicit conversion exists (are you missing a cast?)
问题描述
我正在尝试使用ASP.Net Web API开发一个Web应用程序。现在在创建产品模型和IProductRepository接口后,我很难显示所有产品。我收到错误消息
无法隐式转换类型
System.Collections.Generic.IEnumerable< WebApplication1.Models。 Product>
toSystem.Collections.Generic.IEnumerable< WebApplication1.Product>
。存在明确的转换(您是否缺少转换?)
Controllers \ProductsController.cs 17 20 WebApplication1
在这行代码中
return repo.GetAll();
我不明白要做什么。如果有人指出问题并解释我做错了什么,那将会非常有帮助。
ProductsController.cs
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net;
使用System.Net.Http;
使用System.Web.Http;
使用WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class ProductsController:ApiController
{
static readonly IProductRepository repo = new ProductRepository();
public IEnumerable< Product> GetAllProducts()
{
return repo.GetAll();
}
}
}
ProductRepository.cs
using System;
使用System.Collections.Generic;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
namespace WebApplication1.Models
{
public class ProductRepository:IProductRepository
{
public string cs = System.Configuration.ConfigurationManager.ConnectionStrings [MediaSoftAppEntities ] .ConnectionString;
public IEnumerable< Product> GetAll()
{
列表< Product> products = new List< Product>();
string sql = String.Format(Select [ProductID],[ProductName],[Price] FROM [Products]);
using(SqlConnection con = new SqlConnection(cs)){
using(SqlCommand cmd = new SqlCommand(sql,con)){
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read()){
Product product = new Product();
product.ProductID = dr.GetInt32(0);
product.ProductName = dr.GetString(1);
product.Price = dr.GetInt32(2);
}
}
}
return products.ToArray();
}
}
}
IProductRepository.cs
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
namespace WebApplication1.Models
{
public interface IProductRepository
{
IEnumerable< Product>得到所有();
}
}
错误是说编译器认为
ProductsController.GetAllProducts
想要返回一个 WebApplication1.Product
但 WebApplication1.Models.Product
。
您在这两个不同的命名空间中有2 Product
类吗?
<或者,也许你之前在
WebApplication1
命名空间中有 WebApplication1.Models
命名空间,也许你只是有一个陈旧的参考和重建所有将修复它。 I am trying to develop a web application using ASP.Net Web API. Now after creating Product model and IProductRepository interface I'm having difficulty in showing all products. I'm getting error message
Cannot implicitly convert type
System.Collections.Generic.IEnumerable<WebApplication1.Models.Product>
toSystem.Collections.Generic.IEnumerable<WebApplication1.Product>
. An explicit conversion exists (are you missing a cast?)Controllers\ProductsController.cs 17 20 WebApplication1
on this line of code
return repo.GetAll();
I don't understand what to do. If someone points out the problem and explain me what I'm doing wrong that would be very helpful.
ProductsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class ProductsController : ApiController
{
static readonly IProductRepository repo = new ProductRepository();
public IEnumerable<Product> GetAllProducts()
{
return repo.GetAll();
}
}
}
ProductRepository.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class ProductRepository : IProductRepository
{
public string cs = System.Configuration.ConfigurationManager.ConnectionStrings["MediaSoftAppEntities"].ConnectionString;
public IEnumerable<Product> GetAll()
{
List<Product> products = new List<Product>();
string sql = String.Format("Select [ProductID], [ProductName], [Price] FROM [Products]");
using (SqlConnection con = new SqlConnection(cs)){
using (SqlCommand cmd = new SqlCommand(sql, con)){
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read()){
Product product = new Product();
product.ProductID = dr.GetInt32(0);
product.ProductName = dr.GetString(1);
product.Price = dr.GetInt32(2);
}
}
}
return products.ToArray();
}
}
}
IProductRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public interface IProductRepository
{
IEnumerable<Product> GetAll();
}
}
The error is saying that the compiler thinks that
ProductsController.GetAllProducts
wants to return a WebApplication1.Product
but IProductRepository.GetAll
returns a WebApplication1.Models.Product
instead.
Do you have 2 Product
classes in these 2 different namespaces?
Alternatively, maybe you previously had the Product
class in the WebApplication1
namespace, and it's now been moved into the WebApplication1.Models
namespace, maybe you just have a stale reference and a "Rebuild All" will fix it.
这篇关于不能隐式转换类型。存在明确的转换(您是否缺少演员?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!