Regex regex = new Regex(@"([01]?[0-9]|2[0-3]):[0-5][0-9]"); var output = Exams.Take (100).Where(x=> regex.IsMatch(x.Description));
However, this would result in an error as Sql doesn’t recognize regular expression, which means, Linq wouldn’t be able to convert the statement to an equivalent Sql Query.
This is where the IEnumerable.AsEnumerable method comes into picture. Let’s examine the source of the extension method in Referencesource.
public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source) { return source; }
The method as such, if you were to look up in reference source, might be doing very less, but its significance in the scheme of things is no less important. What it does is that it converts an IQueryable to IEnumerable and there by splits the Linq statement into 2, one that executes on the Server and other, as local collection.
For example, to fix our code above, we would need to change the statement as following.
var output = Exams.Take (100).AsEnumerable().Where(x=> regex.IsMatch(x.Description));
This would split our query into two. The first part would query the Database to fetch 100 rows from Exam table. The second part would then query the resulting collection locally using the Regex expression.
Although the differences are pretty evident once you use it, it is pretty useful to visit the backing classes to understand why it behaves so.
Â