From the perspective of the user the behavior must be understandable. He will not understand why he is getting only 5 records without filter but 25 when he enters a filter. A nice way to solve the problem, is to offer him options in a combo-box or with radio buttons etc., where he can choose the maximum number of records returned. (E.g. 10, 30 or 100). You would always have .Where(filter).Take(maxRecords)
where maxRecords
has been selected by the user.
Any case, the Take(max)
should be performed, whether the user enters a filter or not, to never get into the situation where a filter returns more records than no filter. This can happen, if for instance the user enters a "e" as filter, then probably a high percentage of the entries will be returned.
Also make the result reproducible and comprehensible. Therefore I also suggest to apply an order before .Take(max)
.
with filter
result = source
.Where(filter)
.OrderBy(order)
.Take(max);
or, with no filter
result = source
.OrderBy(order)
.Take(max);