views:

165

answers:

4

I've got a problem with the following code:

public IEnumerable<ISession> GetSessions()
{
    // ...

    using (ProvaDbEntities DBEntities = new ProvaDbEntities(Utilities.ToEntitiesConnectionString()))
    {
        ObjectQuery<session> sessions = DBEntities.session;
        IEnumerable<session> q1 = from session in sessions
                                  where session.site == this.Name
                                  select session;

        List<Session> sessionList = new List<Session>();
        foreach (var s in q1)
        {
            sessionList.Add(new Session(s.id.ToString(),s.username, s.site, new DateTime()));
        }

        IEnumerable<Session> res = sessionList;

        return sessionList;
    }
}

The exception is:

Is not possible to cast object type 'System.Collections.Generic.List`1[prova3.Session]' to type 'System.Collections.Generic.IEnumerable`1[TAP2009.AuctionSite.Interfaces.ISession]'.

Looking at this SO question it seems to be correct. Am I wrong?

A: 

The return type is public IEnumerable<ISession>, i forgot to specify the type of the Ienumerable..

trifabbio
??? I can't put the "<" and ">" character? try again..IEnumerable < ISession >
trifabbio
@trifabbio: You need to tell markdown that it's code, using backticks.
Jon Skeet
+3  A: 

It should be fine, so long as Session implements ISession - if you're using C# 4 and .NET 4. If you're not, it won't be.

Note that the question you referred to use the same "T" in both cases - whereas the exception you've got is about converting a List<Session> to an IEnumerable<ISession>. You haven't stated where you're getting the exception, which makes it a bit harder to see exactly what's going on... Are you sure this is actually the code which is failing? Are you sure you're getting an exception rather than a compile-time failure?

EDIT: If you're not using .NET 4 and C# 4, the workaround for covariance is reasonably simple here - use the Cast<T>() LINQ operator:

return sessionList.Cast<ISession>();
Jon Skeet
Tank you jon, yes, Session implements the Interface Isession.The try - catch block is at the beginning and at the end of the come that I posted.try { using (ProvaDbEntities DBEntities = new ProvaDbEntities(Utilities.ToEntitiesConnectionString()))...Yes, I'm sure, I'getting an exceptionreturn res; } } catch (Exception e) { throw new UnavailableDbException("[GetSessions!!-------------- (site)]" + e.Message); }
trifabbio
@trifabbio: By throwing a new exception you're hiding which line it's being thrown on. (Catching Exception is generally a bad idea too.) The weird thing is that your code doesn't show any casts. Are you sure you don't have a cast in the return statement or assignment to res?
Jon Skeet
Yes jon, I've a cast..List<Session> sessionList = new List<Session>(); foreach (var s in q1) { sessionList.Add(new Session(s.id.ToString(),s.username, s.site, new DateTime())); } IEnumerable<Session> res = sessionList; return (IEnumerable<ISession>)res;If I don't make a cast in the return line I obtain a static error and I can' compile.I commfirm that the class Session Implement the Interface ISession.I'm becaming crazy..
trifabbio
@trifabbio: Right. So the code you've posted isn't the code you've got... *please* don't do that again. How are we meant to debug errors when we can't see your code? I suspect you're using .NET 3.5 or lower - which doesn't support generic covariance - as per my first sentence. It would *also* have been useful if you'd posted what platform you're using from the start. I'll edit my answer with a workaround.
Jon Skeet
I'm sorry jon, you're right. Yes, i'using .NET 3.5. SP1.TanksFabio
trifabbio
A: 

Have you tried using the extension method AsEnumerable()?

So this line

IEnumerable<Session> res = sessionList;

Would change to

IEnumerable<Session> res = sessionList.AsEnumerable();
Duracell
Why would that help? `List<T>` already implements `IEnumerable<T>`.
Jon Skeet
I Duracell, this is not the solution, I stillhave the problem..
trifabbio
A: 

You can add using System.Linq and use the extension method Cast<T> that returns a IEnumerable<T>.

bloparod