C# – Instantiate a IQueryable variable for LINQ

It's not a good image for this article, but I like it
Super LINQ image

Simplicity is the secret for productivity. That is what this post is about.

It’s not a common usage, but sometimes you need to create a instance of a IQueryable variable. I mean, without a call to a LINQ expression or lambda method. You might ask, “why do you want to do this?” I can give you a bunch of reasons, but in my case, I’ve needed to serialize the results to JSON, and “null” was not an option, nor new List.

So, how do I solve it? Simple:

IQueryable listData = Enumerable.Empty<T>().AsQueryable()

It works! This variable serialized produces something like (“listData”: []). Perfect for me!

It has helped a lot and saved a bunch of ifs, new List and some levels of cyclomatic complexity.

Source: http://stackoverflow.com/a/29430798/1209721