无法将类型“System.Collections.Generic.List”隐式转换为“PetShop.Model.It

Jermy120 2012-04-22 10:11:07
public PetShop.Model.Item GetItemById(string ItemId)

{

string sql = "select item.itemid, item.name, item.listprice, product.name, item.image, product.categoryid, product.productid from item inner join product on item.productid = product.productid where item.itemid = @itemid";
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ssa"].ConnectionString;


List<PetShop.Model.Item> list = new List<PetShop.Model.Item>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();

command.Parameters.Add("@itemid", SqlDbType.NVarChar).Value = ItemId;

SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
PetShop.Model.Item item = new PetShop.Model.Item()
{
ItemId = reader.GetString(0),
Name = reader.GetString(1),
Quantity = reader.GetInt32(2),
ListPrice = reader.GetDecimal(3),
ProductName = reader.GetString(4),
Image = reader.GetString(5),
CategoryId = reader.GetString(6),
ProductId = reader.GetString(7)
};
list.Add(item);

}
return list;
}

}
...全文
269 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-04-22
  • 打赏
  • 举报
回复
public PetShop.Model.Item GetItemById(string ItemId)

=>

public List<PetShop.Model.Item> GetItemById(string ItemId)

xboxeer 2012-04-22
  • 打赏
  • 举报
回复
返回类型是item 但是你的返回值是list<item>你说能转换嘛?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧