Aug 30, 2013 2 min read
Today I ran into an interesting issues when writing a unit test for an ExceptionFilter which involved inspecting anonymous return types.
I basically had the requirement to return a Json object with a predefined structure (for our ASP.net WebApi endpoints) in case of exceptions resulting out of failed entity validations. So I started to write a unit test in my Api.UnitTests.csproj project which had a reference to Api.csproj where the actual implementation of the exception filter was. Here’s the skeleton of the exception filter:
One of the unit tests looks like this:
There’s nothing special here, and if you read my post about ASP.net MVC Action Methods: Testing against anonymous return types then you should also not be surprised about the usage of dynamic .
Running the test makes it obviously fail. So let’s complete the implementation:
Re-executing the test shoukld make it pass now…no, it doesn’t..why?? The exception I get:
I started to debug the test and inspected it in the Visual Studio debugger, and look there, the expected object shows up just fine, but any execution results in the above mentioned exception..(?!)
Inspecting in the debugger shows the expected object
Ahhh wait…I’m returning an anonymous type from my exception filter..will that be visible outside my tested DLL “Api”, probably not. Adding
Have you heard about extension methods yet? If you have then you probably know exactly what the title of this blog means. I can see this becoming one of the most asked about exceptions in the coming release. At least until the dust settles.
If you are running into the above exception, you can fix this without reading the entire post. Just add a «usin g System.Linq;» to your using block
if you are seeing the «cannot be inferred from the usage.» exception mentioned below and you are trying to use Linq over DataSets you need to a dd a reference to System.Data.Entity. You will have to hunt for it under the 3.5 framework install directory.
So what is going on? Let’s write some code, a simple Linq over DataSets example that assumes you have the latest ctp installed.
static DataSet GetDataSet()
DataSet ds = new DataSet ();
DataTable dt = new DataTable ( «Customer» );
static void Main( string [] args)
DataSet ds = GetDataSet();
var query = ds.Tables[ «Customer» ].AsEnumerable().Where(c => c.Field string >( «Country» ) == «Country1» )
.Select(c => c.Field string >( «Name» ));
foreach ( string s in query)
For completeness sake I have added a very crude GetDataSet method, please excuse this code bloat but I hate code samples that are not self contained. Once we get a DataSet we do a very simple Linq query to return all the Names of customers living in Country1.
The code looks fairly reasonable, we hit compile and… Exception!:
‘System.Data.DataTable’ does not contain a definition for ‘AsEnumerable’ and no extension method ‘AsEnumerable’ accepting a first argument of type ‘System.Data.DataTable’ could be found (are you missing a using directive or an assembly reference?)
This is a fairly nice exception, readable text and it asks you whether you are missing a using or assembly reference (it turns out we are missing both). If you are reading this blog it is almost certain that you know exactly what the problem is, but let’s pretend that you don’t know what is going on here and do a quick “Microsoft live” search on the exception above. A quick search will almost certainly tell you that you are missing a “using System.Linq” statement. Let’s add this to our code above:
using System.Linq; //add this to the code above.
So, are we done? Not really, this is where things get interesting. When we go to compile the code we get the following gem:
The type arguments for method ‘System.Linq.Enumerable.AsEnumerable (System.Collections.Generic.IEnumerable )’ cannot be inferred from the usage. Try specifying the type arguments explicitly
This is a particularly useless exception. Not only does it make almost no sense, the suggestions is horrible (do not try to specify the type arguments explicitly). What went wrong?
The problem is that for the nice linq over DataSet code above to work we need to use the System.Data.Entity.dll EnumerableDataTable. To do this we need to add a reference to System.Data.Entity.dll in our project. Unfortunatelly there is no good way for System.Linq to throw a better exception here, it just can’t know that you are trying to use the wrong AsEnumerable.
//Add System.Data.Entity.dll to the list of References of your project.
Finally your project will compile and run as expected, congratulations!
Ok, let’s go for some extra credit. Now that we have determined that we really needed to add the System.Data.Entity.dll reference why don’t we just remove the “using System.Linq” statement?
Very simple, because if we remove System.Linq we get the following exception:
‘System.Collections.Generic.IEnumerable ‘ does not contain a definition for ‘Where’ and no extension method ‘Where’ accepting a first argument of type ‘System.Collections.Generic.IEnumerable ‘ could be found (are you missing a using directive or an assembly reference?)
The reason for this is left as an exercise to the reader (I have always wanted to say that *grin*)

Camera.main — это шорткат для получения камеры с тегом «MainCamera». Т.е. вот эти две строки аналогичны:
Вероятно вы удалили камеру, которую Unity добавляет в сцену автоматически. Или переименовали тег.