我正在使用Linq to SQL和我的控制器類中的MVC 4我打算從具有多個表的查詢中獲取List並將其顯示到視圖中作爲一個列表,其中M是一個模型類:將匿名類型轉換爲列表<T>使用Linq to SQL在C#
在控制器我有這個
public List<HomeViewModel> GetHomeViewModel()
{
dynamic ReservationDay = (from c in hutRes.Clients
join r in hutRes.Reservations on c.ID_Client equals r.FK_Client
join ht in hutRes.Hut_Types on r.FK_HutType equals ht.ID_Hut_Type
join tr in hutRes.Time_Reserves on r.FK_TimeReserve equals tr.ID_Time_Reserve
join tc in hutRes.Type_ClientIds on c.FK_TypeClientId equals tc.ID_Type_ClientId
where r.DateR == DateTime.Now
select new
{
r.ID_Reservation,
tc.ClientId,
Number = c.Number_ID,
c.Name,
c.Phone,
time = tr.TimeReserve,
ReservationDate = r.DateR
}).ToList();
return ReservationDay;
}
public ActionResult Index()
{
return View(GetHomeViewModel());
}
而在觀看側我有這樣的報頭:
@model List<HutReservation.ViewModel.HomeViewModel>
我在此錯誤:
Can not implicitly convert type 'System.Collections.Generic.List<<>f__AnonymousType4 <int,string,short,string,string,string,System.DateTime,byte,string,short,string,string>>' in 'System.Collections.Generic.List<HutReservation.ViewModel.HomeViewModel>'
任何人都可以幫我我真的被困在這裏:(
Sorrymy錯誤意味着: 無法含蓄轉化類型:「System.Collections.Generic.List <<> f__AnonymousType4 <整型,字符串,短,字符串,字符串,字符串,System.DateTime,字節,字符串,短,字符串,字符串>>'in'System.Collections.Generic.List' –
Lebarros
創建匿名類型而不是'HomeViewModel'的原因是什麼? –