我在控制器通過LINQ查詢與組通過查看[錯誤出]
var tobi = (from a in db.Assessments
join u in db.Users on a.rated equals u.user_id
join tm in db.Team_Members on u.user_id equals tm.user_id
join t in db.Teams on tm.team_id equals t.team_id
where (tm.end_date == null || tm.end_date > DateTime.Today)
group new TobiViewModel
{
teamname = t.name,
assessed_due = a.assessed_due,
assessment_id = a.assessment_id,
rater = a.rater,
rated = a.rated
}
by new { t.name, a.rated }).ToList();
return View(tobi);
得到這個但後來它不認爲接受呢?
傳遞到詞典中的模型產品 類型的 'System.Collections.Generic.List
1[System.Linq.IGrouping
2 [<> f__AnonymousType102[System.String,System.String],Kyubu.ViewModels.TobiViewModel]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [Kyubu.ViewModels.TobiViewModel]'。
我應該如何通過這個視圖。任何幫助,將不勝感激 視圖中的類型是
@model IEnumerable<Kyubu.ViewModels.TobiViewModel>
我認爲當你將鼠標懸停在VAR它說什麼「TOBI」的實際類型?然後,您可以在該視圖中使用該類型作爲模型(而不是IEnumerable) –
Michel
當您在linq中進行分組時,您將得到一個IGrouping,其中T是您分組的內容(在本例中爲帶有2個字符串的匿名類型屬性),U是在這裏分組的TobiViewModel。 –
這裏的主要問題是您的密鑰是匿名類型,因此您將無法將其指定爲模型類型。如果您可以使用已知類型或動態類型來提供您的密鑰類型,那麼解決問題應該會更容易。 – Wasp