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's focused on data and business layers, language constructs, parallelism, databases and performance. He's Microsoft Most Valuable Professional and frequent speaker. You can read his articles, guides and tips and tricks at www.tabsoverspaces.com.