Ali Tor
Ali Tor

Reputation: 2995

How to write a LINQ query if index usage is necessary?

I have a method to get information about if a URL source supports Accept-Ranges.

The method is:

bool getAcceptRangeHeaderValue()
{
    for (int i = 0; i < resp.Headers.AllKeys.Count; i++)
    {
        if (resp.Headers.AllKeys[i].Contains("Range"))
            return resp.Headers[i].Contains("byte");
    }
    return false;
}

I want to write the method in LINQ to be shorter. But I couldn't do it because of the index usage. How to write it in LINQ?

Upvotes: 0

Views: 43

Answers (1)

Rob
Rob

Reputation: 27367

It's not really shorter, but if you really want to use LINQ, you can write:

return a.Headers.AllKeys
    .Select((v, ind) =>
    new {
        HeaderName = v,
        HeaderValue = a.Headers[ind],
    })
    .Any(g => g.HeaderName.Contains("Range") && g.HeaderValue.Contains("byte"))

Upvotes: 3

Related Questions