Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Types in a signature

I am trying to get the signature on the method below to work. As this is an Anonymous Type I have some trouble, any help would be great.

When I looked at sortedGameList.ToList() in a QuickWatch window I get the signature

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

Many Thanks

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();

}
like image 357
Donald Avatar asked Oct 26 '08 14:10

Donald


2 Answers

You shouldn't return anonymous instances.

You can't return anonymous types.

Make a type (named) and return that:

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;
}
like image 63
Amy B Avatar answered Sep 23 '22 00:09

Amy B


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

Edit: thats wrong! I think this will do.

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

    return sortedGameList.ToList();
}

And no, you do not need the select!

like image 39
leppie Avatar answered Sep 20 '22 00:09

leppie