无法将类型'System.Colections.Generic.IEnuerable <product>'隐式转换为'product'。存在显式转换

时间:2018-04-16 06:35:46

标签: asp.net-mvc generic-exception

我是asp.net MVC的新手。我在尝试分配时遇到以下错误:

无法将类型'System.Colections.Generic.IEnuerable'隐式转换为'product'。存在显式转换

我的代码是

select SUM(Book.price * Orderline.quantity) 
FROM customer 
INNER JOIN salesorder  ON customer.cno = salesorder.cno
INNER JOIN Orderline  ON salesorder.ono = Orderline.ono
INNER JOIN Book  ON Book.bno = Orderline.bno
where customer.zip IN (95124,95380,95382);

其中product是具有属性的类

ILookup<string,product> productList = Model.ToLookup(x=>x.parentId,x=>x);
IEnumerable<string> roots =Model.Select(x=>x.parentId).Except(Model.Select(x=>x.id));
foreach(var id in roots){
    product pr = productList[id].Select(item => item);//Error here

}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

更改此

product pr = productList[id].Select(item => item);

product pr = productList[id].Select(item => item).First();

或者更简单地说,

product pr = productList[id];

每当编写如下代码:something.Select(t=>t)时,它应删除Select语句并成为something