tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Extending IQueryable with superset method and StackOverflowException

14 Jan 2010 1 mins 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 .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.