簡體   English   中英

無法通過linq C#將類型system.collection.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.stationIEnumerable ,您正嘗試將其存儲在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM