Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a maximum number of items the LINQ way

Tags:

c#

linq

Suppose I have a search module with an underlying data repository, and a requirement to return a maximum of 25 results from the search query. I can enforce this with a Take() operation:

IEnumerable<Contact> Search(string name)
{
    // validation/cleanup on name parameter

    IEnumerable<Contact> matching = _repository.Search(name);
    return matching.Take(25);
}

Next, suppose I have an additional requirement to throw an exception if more than 25 results would be returned (i.e. the search parameter is too broad). Is there a straightforward way to do this with LINQ? The closest I have come so far is to take one more than the maximum number and work with that:

IEnumerable<Contact> Search(string name)
{
    // validation/cleanup on name parameter

    var matching = _repository.Search(name);
    var toReturn = matching.Take(26).ToList();
    if (toReturn.Count() > 25)
    {
        throw new Exception("Too many results");
    }

    return toReturn;
}

However, this seems a bit clunkier than necessary.

like image 217
Eric Pohl Avatar asked Jan 13 '23 00:01

Eric Pohl


1 Answers

Your method is the best method. I would not make any changes at all.

Any other option, such as querying the count alone first is performing what appears to be an expensive operation (doing the actual searching) twice in the event that you have less than 26 items. You save only a little tiny bit in the error case and add a significant expense in the common case.

The only time your code would be less desirable is if _repository.Search(name) returned a type that could be cheaply iterated twice and that could cheaply provide it's count (like, say, a List) but in context that doesn't appear to be the case.

like image 61
Servy Avatar answered Jan 21 '23 05:01

Servy