Pregunta

Estoy tratando de obtener la firma en el siguiente método para que funcione. Como este es un tipo anónimo, tengo algunos problemas, cualquier ayuda sería genial.

Cuando miré sortedGameList.ToList () en una ventana de QuickWatch, recibí la firma

System.Collections.Generic.List<<>f__AnonymousType0<System.DateTime,System.Linq.IGrouping<System.DateTime,DC.FootballLeague.Web.Models.Game>>>

Muchas gracias

Donald

   public List<IGrouping<DateTime, Game>> getGamesList(int leagueID)
{
    var sortedGameList =
        from g in Games
        group g by g.Date into s
        select new { Date = s.Key, Games = s };

    return sortedGameList.ToList();

}
¿Fue útil?

Solución

select new {Date = s.Key, Games = s.ToList ()};

Editar: eso está mal! Creo que esto servirá.

public List<IGrouping<DateTime, Game>> getGamesList(int leagueID)
{
    var sortedGameList =
        from g in Games
        group g by g.Date;

    return sortedGameList.ToList();
}

¡Y no, no necesita seleccionar!

Otros consejos

No debe devolver instancias anónimas.

No puede devolver tipos anónimos.

Cree un tipo (con nombre) y devuelva eso:

public class GameGroup
{
  public DateTime TheDate {get;set;}
  public List<Game> TheGames {get;set;}
}

//

public List<GameGroup> getGamesGroups(int leagueID)
{
  List<GameGroup> sortedGameList =
    Games
    .GroupBy(game => game.Date)
    .OrderBy(g => g.Key)
    .Select(g => new GameGroup(){TheDate = g.Key, TheGames = g.ToList()})
    .ToList();

  return sortedGameList;
}

La respuesta simple es: no use un tipo anónimo.

Lo más cercano que obtendrá con ese tipo anónimo es IEnumerable < object > ;. El problema es que cualquiera que use tus cosas no sabrá qué hacer con ese objeto cuyo tipo era & Quot; impredecible & Quot ;.

En cambio, crea una clase como:

public class GamesWithDate {
    public DateTime Date { get; set; }
    public List<Game> Games { get; set; }
}

Y cambie su LINQ a:

var sortedGameList =
    from g in Games
    group g by g.Date into s
    select new GamesWithDate { Date = s.Key, Games = s };

Ahora estás devolviendo List < GamesWithDate > ;.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top