Problem returning an IEnumerable<T>/IList<T>
我很难返回一个IEnumerable和IList,我做不到!我正在使用EF4和POCO
整个方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //public IList<Genre> GetGenresByGame(int gameId) public IEnumerable<Genre> GetGenresByGame(int gameId) { using(var ctx = new XContext()) { var results = from t0 in ctx.GameGenres join t1 in ctx.GenreCultureDetails on t0.GenreId equals t1.GenreId where t0.GameId == gameId && t1.CultureId == _cultureId select new Genre { GenreId = t0.GenreId, GenreName = t1.GenreName }; return results.ToList(); } } |
我尝试过在网上找到的不同方法。但不能让它工作!
问题2:我看到一个电影演员和朱莉在一起,说在使用EF4的时候"你应该总是返回一个回忆"。
有什么想法吗?
溴
编辑:当我以调试模式加载页面时,我得到这些错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public IEnumerable<Genre> GetGenresByGame(int gameId) { using(var ctx = new XContext()) { var resultList = from t0 in ctx.GameGenres join t1 in ctx.GenreCultureDetails on t0.GenreId equals t1.GenreId where t0.GameId == gameId && t1.CultureId == _cultureId select new { t0.GenreId, t1.GenreName }; var genres = resultList.AsEnumerable().Select(o => new Genre { GenreId = o.GenreId, GenreName = o.GenreName }); return genres.ToList(); } } |
首先,如果流派在数据库中,您应该选择它吗?如果您有来自流派的FK->GenRecurtureDetails,请告诉我,我可以更新以下内容,但从外观上看,您可以这样做:
1 2 3 4 5 6 7 8 9 10 11 | using(var ctx = new XContext()) { var results = from g in ctx.Genre join gcd in ctx.GenreCultureDetails on g.GenreId equals gcd.GenreId where g.GameId == gameId && gcd.CultureId == _cultureId select g; return result.ToList(); } |
或者继续沿着您的路径,将它们选择成烦人的类型,然后复制它们。如果需要,可以使用select而不是convertall。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | IList<Genre> returnMe = Null; using(var ctx = new XContext()) { var results = from t0 in ctx.GameGenres join t1 in ctx.GenreCultureDetails on t0.GenreId equals t1.GenreId where t0.GameId == gameId && t1.CultureId == _cultureId select new { GenreId = t0.GenreId, GenreName = t1.GenreName }; returnMe = results.ToList().ConvertAll(x=>new Genre(){ GenreId = x.GenreId, GenreName = x.GenreName } ); } return returnMe; |