2012-10-23 14 views
5

मैं एक LINQ क्वेरी जो मैं देखने के लिए पास करना चाहते हैं बनाया है, लेकिन यह मुझे इस त्रुटिपास LINQ क्वेरी

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType4`2[System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1 

मेरे ViewModel

public class ManageScoreViewModel 
    { 
     public string fullName { get; set; } 
     public string teamName { get; set; } 
    } 

नियंत्रक

public ActionResult Index() 
     { 
      MyteamViewModel ms = new MyteamViewModel(); 
      var vm = (from u in db.Users 
        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 
        orderby t.name 
        select new { fullName = u.firstName + u.lastName, teamName = t.name }); 
      // var vmlist = vm.ToList(); 

      return View(vm); 
     } 
दे रहा है देखने के लिए

दृश्य टाइप किया गया है IENumerable

@model IEnumerable<CTA2._0.ViewModels.ManageScoreViewModel> 
@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.fullName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.teamName) 
     </td> 

मैंने क्वेरी को एक सूची में बदलने की कोशिश की (var vmlist = vm.ToList();)। काम नहीं किया किसी भी सहायता की सराहना की जाएगी।

उत्तर

6

आपका विचार दृढ़ता से ManageScoreViewModel संग्रह पर टाइप किया गया है लेकिन आपकी linq क्वेरी प्रक्षेपण का उपयोग कर अज्ञात वस्तु को वापस कर रही है। आपको ManageScoreViewModel ऑब्जेक्ट्स की एक सूची वापस करने की आवश्यकता है। तुम भी

बदलें

select new { fullName = u.firstName + u.lastName, teamName = t.name }); 

select new ManageScoreViewModel { fullName = u.firstName + u.lastName, 
                teamName = t.name }).ToList(); 
को LINQ अभिव्यक्ति पर ToList() विधि लागू कर सकते हैं