C# 无法隐式转换类型。存在显式转换(是否缺少强制转换?)
我正在尝试使用ASP.NETWebAPI开发一个web应用程序。现在,在创建产品模型和IPProductRepository界面之后,我很难显示所有产品。我收到了错误信息 无法将类型C# 无法隐式转换类型。存在显式转换(是否缺少强制转换?),c#,asp.net,.net,generics,C#,Asp.net,.net,Generics,我正在尝试使用ASP.NETWebAPI开发一个web应用程序。现在,在创建产品模型和IPProductRepository界面之后,我很难显示所有产品。我收到了错误信息 无法将类型System.Collections.Generic.IEnumerable隐式转换为System.Collections.Generic.IEnumerable。存在显式转换(是否缺少强制转换?) 控制器\ProductsController.cs 17 20 Web应用程序1 在这行代码上 返回repo.GetA
System.Collections.Generic.IEnumerable
隐式转换为System.Collections.Generic.IEnumerable
。存在显式转换(是否缺少强制转换?)
控制器\ProductsController.cs 17 20 Web应用程序1
在这行代码上
返回repo.GetAll()代码>
我不知道该怎么办。如果有人指出问题并向我解释我做错了什么,那将非常有帮助
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();
}
}
}
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();
}
}
}
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web.Http;
使用WebApplication1.模型;
命名空间WebApplication1.Controllers
{
公共类产品控制器:ApiController
{
静态只读IPProductRepository repo=new ProductRepository();
公共IEnumerable GetAllProducts()
{
返回repo.GetAll();
}
}
}
ProductRepository.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();
}
}
}
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();
}
}
}
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
命名空间WebApplication1.Models
{
公共类ProductRepository:IPProductRepository
{
公共字符串cs=System.Configuration.ConfigurationManager.ConnectionString[“MediaSoftAppenties”]。ConnectionString;
公共IEnumerable GetAll()
{
列表产品=新列表();
string sql=string.Format(“从[Products]中选择[ProductID]、[ProductName]、[Price]”;
使用(SqlConnection con=newsqlconnection(cs)){
使用(SqlCommand cmd=newsqlcommand(sql,con)){
con.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read()){
产品=新产品();
product.ProductID=dr.GetInt32(0);
product.ProductName=dr.GetString(1);
产品价格=dr.GetInt32(2);
}
}
}
返回产品。ToArray();
}
}
}
IProductRepository.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();
}
}
}
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();
}
}
}
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间WebApplication1.Models
{
公共接口存储库
{
IEnumerable GetAll();
}
}
错误是编译器认为
ProductsController.GetAllProducts
想要返回一个WebApplication1.Product
但是IProductRepository.GetAll
返回一个WebApplication1.Models.Product
在这两个不同的名称空间中是否有2个Product
类
或者,可能您以前在WebApplication1
名称空间中有Product
类,现在它被移动到WebApplication1.Models
名称空间中,可能您只是有一个过时的引用,“全部重建”将修复它。如错误所述,我想你们有两个同名产品的类。检查整个项目是否有两个同名的类;基兰赫德说了些什么。也许您在某个地方有一个部分声明,但它的名称空间是错误的?多亏了您,问题已经解决了。我错误地将ADO.Net实体数据模型添加到根文件夹中,而不是模型文件夹中。所以它在根文件夹中自动生成了3个模型,我没有注意到。我手动添加了产品模型,因此引发了冲突。现在,在删除复制并在模型文件夹中添加数据模型后,所有这些都可以正常工作。多亏了您,问题解决了。我错误地将ADO.Net实体数据模型添加到根文件夹中,而不是模型文件夹中。所以它在根文件夹中自动生成了3个模型,我没有注意到。我手动添加了产品模型,因此引发了冲突。现在,在删除复制并在模型文件夹中添加数据模型之后,所有这些都可以正常工作。