tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Extending IQueryable with superset method and StackOverflowException

14 Jan 2010 Entity Framework, LINQ

Yesterday I was creating extension method for Skip for IQueryable. I needed to accept the nullable integer and if null just skip any processing. Something like this:

static IQueryable<T> Skip<T>(this IQueryable<T> resource, int? count)
{
	return (count.HasValue ? resource.Skip(count.Value) : resource);
}

I thought it will pick the int version from framework, because I’m calling it with int and not int?. And I was wrong, as my colleague uncovered moments later. How to solve it? I was not happy with method rename and I felt there’s a way.

Actually the solution is pretty easy. The original Skip is just extension method, right? So it’s in static class as static method. So I can call it with class definition as non-extension method and this this there will be no ambiguity. Voilà.

static IQueryable<T> Skip<T>(this IQueryable<T> resource, int? count)
{
	return (count.HasValue ? Queryable.Skip(resource, count.Value) : resource);
}

Profile Picture Jiří Činčura is an independent developer focusing on data and business layers, language constructs, parallelism and databases. Specifically Entity Framework, asynchronous and parallel programming, cloud and Azure. He's Microsoft Most Valuable Professional and you can read his articles, guides, tips and tricks at www.tabsoverspaces.com.