开发者

Problem returning an IEnumerable<T>/IList<T>

I am having trouble to return an IEnumerable and IList, I cant do it! I am using EF 4 with POCOs

Here's the whole method:

//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.Genre开发者_运维技巧Id,
                        GenreName = t1.GenreName

                    };

            return results.ToList(); 
        }
    }

I have tried different ways that I have found on the net.. but can't make it work!

Question 2: I saw a screencast with Julie something, saying that "You should always return an ICollection" when using EF4.

Any thoughts about that ?

BR

EDIT: When I load the page in Debug-mode i get these errors: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. OR The entity or complex type 'XModel.Genre' cannot be constructed in a LINQ to Entities query.


Genre must not be a L2EF type. Try this:

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


First an foremost if Genre is in the database you should select it? If you have FKs from Genre->GenreCultureDetails let me know and I can update the below, but from the looks of it you could do it like this:

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

Alternatively continue down your path select them into an annoynmous type, and then copy them. You can use select instead of convertall if you please.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜