Ali Tor Ali Tor - 2 months ago 11
C# Question

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?

Rob Rob

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"))