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: 
I ran into this error while using the "Dynamic data access framework" [Passive library](https://github.com/Talljoe/Passive). The source of the error was line 100 in the DynamicDatabase.cs file.

```
databaseDetectors = (databaseDetectors ?? Enumerable.Empty<DatabaseDetector>()).DefaultIfEmpty(new DatabaseDetector());

```

I changed that line of code to:

```
databaseDetectors = (databaseDetectors ?? Enumerable.Empty<DatabaseDetector>()).DefaultIfEmpty(new DatabaseDetector()).OfType<IDatabaseDetector>();

```

Doing so resolved the problem. I went ahead and [forked the project](https://github.com/Jabberrwocky/Passive) and submitted the change to the original author.

Thank you, Jason Baker, for pointing out the solution in your original question.

On a side note, the original library ran fine on my local machine and on a Rackspace VPS, but when I pushed the same code to a shared hosting environment (GoDaddy and Rackspace's Cloud Sites), I began getting the "Operation could destabilize the runtime" error.
I guess, Linq to Sql may not support casting when translate to sql statement.