Q:

How to use LINQ to query a subset of string

Suppose I have an unordered List<String> named letters:
letters. Add("d.pdf");
letters. Add("a.pdf");
letters. Add("c.pdf");
letters. Add("b.pdf");
letters. Add("e.pdf");
letters. Add("f.pdf");

I want to order that list alphabetically and then, take the elements between b and d (including the endpoints), so this will return a new list like {"b.pdf","c.pdf","d.pdf"}. 

A:

Simple foreach loop solution:
var letters = new List<string> {"d", "a", "c", "b", "e"};

            letters. Sort();

            var start = "b";
            var end = "d";
            var r = new List<string>();
            foreach (var l in letters)
            {
                if (l.CompareTo(start) >= 0 && l.CompareTo(end) <= 0)
                {
                    r.Add(l);
                }
            }

another simple solution:
var letters = new List<string> {"d", "a", "c", "b", "e"};
letters. Sort();
 var startIndex = letters. IndexOf(letters. Find(l=> l.CompareTo(start) == 0));
 var endIndex = letters. IndexOf(letters. Find(l => l.CompareTo(end) == 0));
 var r = letters. GetRange(startIndex, endIndex - startIndex + 1);

If you use TakeWhile, this solution should work, this is more Linq like.  The main problem would be to have the starting element in the first index of collection, otherwise TakeWhile won't work. 
    var letters = new List<string> { "d", "a", "c", "b", "e" };
    letters. Sort();

    var start = "b";
    var end = "d";
    var startIndex = letters. IndexOf(letters. Find(l=> l.CompareTo(start) == 0));
    var r = letters. GetRange(startIndex, letters. Count - startIndex)
              .TakeWhile(l => l.CompareTo(start) >= 0 && l.CompareTo(end) <= 0).ToList();

