2

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?

Community
  • 1
  • 1
trifabbio
  • 21
  • 1
  • 2

4 Answers4

5

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 exception return res; } } catch (Exception e) { throw new UnavailableDbException("[GetSessions!!-------------- (site)]" + e.Message); } – trifabbio Jun 19 '10 at 15:43
  • @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 Jun 19 '10 at 15:46
  • Yes jon, I've a cast.. List sessionList = new List(); foreach (var s in q1) { sessionList.Add(new Session(s.id.ToString(),s.username, s.site, new DateTime())); } IEnumerable res = sessionList; return (IEnumerable)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 Jun 19 '10 at 16:19
  • @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 Jun 19 '10 at 16:29
  • I'm sorry jon, you're right. Yes, i'using .NET 3.5. SP1. Tanks Fabio – trifabbio Jun 19 '10 at 17:48
2

Have you tried using the extension method AsEnumerable()?

So this line

IEnumerable<Session> res = sessionList;

Would change to

IEnumerable<Session> res = sessionList.AsEnumerable();
duraz0rz
  • 387
  • 1
  • 2
  • 10
0

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
trifabbio
  • 21
  • 1
  • 2
0

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

bloparod
  • 1,716
  • 1
  • 14
  • 17