-
04-07-2019 - |
문제
아래의 방법에 서명을 얻으려고 노력하고 있습니다. 이것은 익명의 유형이기 때문에 문제가 있는데 도움이 될 것입니다.
QuickWatch 창에서 SortedGamelist.tolist ()를 보았을 때 서명을 얻습니다.
System.Collections.Generic.List<<>f__AnonymousType0<System.DateTime,System.Linq.IGrouping<System.DateTime,DC.FootballLeague.Web.Models.Game>>>
많은 감사합니다
도널드
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();
}
해결책
새 {date = s.key, games = s.tolist ()}를 선택하십시오.
편집 : 그게 잘못되었습니다! 나는 이것이 할 것이라고 생각한다.
public List<IGrouping<DateTime, Game>> getGamesList(int leagueID)
{
var sortedGameList =
from g in Games
group g by g.Date;
return sortedGameList.ToList();
}
그리고 아니, 당신은 선택이 필요하지 않습니다!
다른 팁
익명 인스턴스를 반환해서는 안됩니다.
익명 유형을 반환 할 수 없습니다.
유형 (이름)을 만들고 반환하십시오.
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;
}
간단한 대답은 : 익명 유형을 사용하지 마십시오.
익명 유형으로 가장 가까운 것은 ienumerable입니다.u003Cobject> . 문제는, 당신의 물건을 사용하는 사람이라면 누구나 유형이 "예측할 수없는"객체로 무엇을 해야할지 모를 것입니다.
대신, 수업을 : 다음과 같이 만드십시오.
public class GamesWithDate {
public DateTime Date { get; set; }
public List<Game> Games { get; set; }
}
LINQ를 다음으로 변경하십시오.
var sortedGameList =
from g in Games
group g by g.Date into s
select new GamesWithDate { Date = s.Key, Games = s };
이제 목록을 반환하고 있습니다u003CGamesWithDate> .
제휴하지 않습니다 StackOverflow