[英]Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable
[英]cannot implicitly convert type system.collection.generic IEnumerable to models in group by linq c#
我在C#項目中遇到錯誤,這使我頭疼。 錯誤是:
Cannot implicitly convert type 'System.Collecitons.Generic.IEnumbrable<Models.tbl_station>'
to Models.tbl_station An explicit conversion exists(are u missing a cast)
這是我的代碼。
var results =
(from p in db.tbl_pageDetail
group p by new { p.station_id, p.category_id } into g
let pageno = (from i in g select i.pageNo)
let station = (from i in g select i.tbl_station)
select new
{
g.Key.category_id,
g.Key.station_id,
pageno,
station
}).ToList();
var data =
results.Select(x =>
new tbl_pageDetail
{
category_id = x.category_id,
pageNo = string.Join(", ", x.pageno),
station_id = x.station_id,
tbl_station = x.station // Here i getting error
});
return View(data);
您的錯誤信息不言自明! x.station
是IEnumerable
,您正嘗試將其存儲在tbl_station
。 只要考慮你自己的例子:
pageNo = string.Join(", ", x.pageno)
由於x.pageno是IEnumerable,因此您可以將其傳遞給Join
方法以創建單個項目, tbl_station = x.station
也是這種情況,這里有一個List,因此您無法將其存儲到單個對象中,要么需要IEnumerable的tbl_station,要么需要使用FirstOrDefault()
或SingleOrDefault()
從x.station獲取單個項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.