Pergunta

Estou tentando obter a assinatura no método abaixo para funcionar. Como este é um tipo anônimo, tenho alguns problemas, qualquer ajuda seria ótima.

Quando olhei para o StreedGameList.tolist () em uma janela do Quickwatch, recebo a assinatura

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

Muito obrigado

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

}
Foi útil?

Solução

selecione novo {date = s.Key, jogos = s.tolist ()};

EDIT: Isso está errado! Eu acho que isso vai servir.

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

    return sortedGameList.ToList();
}

E não, você não precisa da seleção!

Outras dicas

Você não deve devolver instâncias anônimas.

Você não pode devolver tipos anônimos.

Faça um tipo (nomeado) e devolva que:

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;
}

A resposta simples é: não use um tipo anônimo.

O mais próximo que você vai chegar com esse tipo anônimo é Ienumerableu003Cobject> . O problema é que qualquer pessoa que use suas coisas não saberá o que fazer com esse objeto cujo tipo era "imprevisível".

Em vez disso, faça uma aula como:

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

E mude seu LINQ para:

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

Agora você está de volta à listau003CGamesWithDate> .

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top