Question: I'm having a little bit of trouble understanding what the problem is here. I have a bit of code that pulls records from a database using LINQ and puts them into an object which is cast into an interface. It looks a bit like this:

```
public IEnumerable<ISomeObject> query()
{
    return from a in dc.SomeTable
           select new SomeObject
           {
             //Assign various members here
           } as ISomeObject;
}

```

When I test this, I put the returned IEnumerable into a variable called results and run this line:

```
Assert.AreEqual(EXPECTED_COUNT, results.Count());

```

When this is run, I get a System.Security.VerificationException: "Operation could destabilize the runtime."

I found the solution [here](http://devlicio.us/blogs/derik_whittaker/archive/2008/11/29/quot-operation-could-destabilize-the-runtime-quot-from-casting-from-concrete-to-interfaces-with-linq.aspx), which is this:

```
var results = from a in dc.SomeTable
              select new SomeObject
              {
                //Assign various members here
              } as ISomeTable;
return results.OfType<ISomeObject>();

```

This works, but I'm having trouble understanding what's happening here. Why did I get the exception in the first place and how did the lines of code above fix it? The MSDN documentation seems to suggest that this is an issue of type safety, but I'm not seeing where the previous code was type-unsafe.

**UPDATE**
A little bit more information I found out. The first example works if I make the return type IQueryable. This sheds a little bit more light on *what* was going wrong, but I'm still confused about the *why*. Why didn't the compiler force me to cast the IEnumerable into an IQueryable?

Answer: 
Just a guess, but the **as** operator may return a null - so it may have to do with the actual implementation of the `new SomeObject { ... }` code, since it's syntactic sugar. The `return results.OfType<ISomeTable>();` filters based on type, so your method's return statement will only return that type (ensuring type safety). I've run into a similar issue with returning generic types.

P.S. I love the "Operation could destabilize the runtime." exception. That's almost like the "You might blow up the internet" exception.
I found that OfType had some nasty side effects when using linq to sql. For example, parts of the linq that were previously evaluated after the query was run against the db were instead translated to SQL. This failed as those sections had no SQL equivalent. I ended up using .Cast instead which seems to solve the problem as well.