2011-08-27 22 views
6

için bir LINQ içinde yapılamıyor Bu hata "System.NotSupportedException: varlık veya Karmaşık türü 'MyModel.Team' bir LINQ varlık varlıkları sorgu oluşturulamıyor." Ekip/Dizin/{id} sayfasına gittiğimde. Birisi beni yaptığım hataya yönlendirebilir mi?Hata: Varlık veya karmaşık türü varlıklar sorgu

Denetleyici:

public ActionResult Index(int id) 
    { 
     IQueryable<Team> teams = teamRepository.GetTeamByPersonID(id); 
     return View("Index", teams); 
    } 

Depo:

public IQueryable<Team> GetTeamByPersonID(int id) 
    { 
     return from t in entities.Teams 
       join d in entities.Departments 
       on t.TeamID equals d.TeamID 
       where (from p in entities.Person_Departments 
         join dep in entities.Departments 
         on p.DepartmentID equals dep.DepartmentID 
         where p.PersonID == id 
         select dep.TeamID).Contains(d.TeamID) 
       select new Team 
       { 
        TeamID = t.TeamID, 
        FullName = t.FullName, 
        ShortName = t.ShortName, 
        Iso5 = t.Iso5, 
        DateEstablished = t.DateEstablished, 
        City = t.City, 
        CountryID = t.CountryID 
       }; 
    } 

ViewModel:

public IQueryable<Team> teamList { get; set; } 
public TeamViewModel(IQueryable<Team> teams) 
    { 
     teamList = teams; 
    } 

Görünüm:

<% foreach (var team in Model){ %> 
    <tr> 
     <td><%: Html.ActionLink(team.ShortName, "Details", new { id=team.TeamID}) %></td> 
     <td><%: team.City %></td> 
     <td><%: team.Country %></td> 
    </tr> 
<% } %> 

cevap

10

Sorun, LINQ to SQL tarafından desteklenmeyen bir select deyiminde Team sınıfı oluşturuyor olmanızdır.

select t 

veya anonim türü kullanmak: senin select değiştirin

select new 
{ 
    TeamID = t.TeamID, 
    FullName = t.FullName, 
    ShortName = t.ShortName, 
    Iso5 = t.Iso5, 
    DateEstablished = t.DateEstablished, 
    City = t.City, 
    CountryID = t.CountryID 
}; 

veya DTO (bir varlık değildir şey) kullanın:

select new TeamDTO 
{ 
    TeamID = t.TeamID, 
    FullName = t.FullName, 
    ShortName = t.ShortName, 
    Iso5 = t.Iso5, 
    DateEstablished = t.DateEstablished, 
    City = t.City, 
    CountryID = t.CountryID 
}; 
+0

Detaylı cevap için teşekkür ederiz! – Tsarl

4

sınıf Team bir ise Varlık linq ifadesi içinde oluşturulamaz. Kendi sınıfınızı oluşturmayı ve bunun yerine döndürmeyi düşünmelisiniz. Ya da belki sadece select t.

+0

Doğru cevap için teşekkürler. – Tsarl

+1

Sebebi herhangi biri açıklanabilir mi? –

İlgili konular