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();
}
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;
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With