por Cesar Cassiano Schimanco

Unable to cast object of type WhereListIterator 1[...] to type System.Collections.Generic.List 1[...]

Para resolver o erro abaixo é simples, ao invés de fazer a converção explicita (List<Menu>), basta adicionar .ToList() no final que resolve o problema, veja o exemplo:

Antes

List<Menu> _menus = (List<Menu>)(from menu in menus where menu.Parent == 0 select menu);

Depois

List<Menu> _menus = (from menu in menus where menu.Parent == 0 select menu).ToList();

 

Server Error in '/site' Application.

Unable to cast object of type 'WhereListIterator`1[Menu]' to type 'System.Collections.Generic.List`1[Menu]'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'WhereListIterator`1[Menu]' to type 'System.Collections.Generic.List`1[Menu]'.

Source Error:

Line 76:         else
Line 77:         {
Line 78:             List<Menu> _menus = (List<Menu>)(from menu in menus where menu.Parent == 0 select menu); Line 79:             if (_menus != null)
Line 80:                 return _menus;

 

Comentários

Carregando comentários

Postar um novo comentário



Processando...