Linq query - find strings based upon first letter b/w two ranges

17,038

Solution 1

var countryAG = from elements in countryList
                where elements[0] >= 'A' && elements[0] <= 'H'
                select elements;

Chars are just numbers really, thus you can compare them as such

Solution 2

I can't test it right now, but I would try

countryList.Where((s) => s[0] <= 'A' && s[0] >= 'G');

Solution 3

Try

char[] startingLetters = new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
var countryAG = 
    from elements in countryList 
    where elements.IndexOfAny(startingLetters, 0, 1) == 0 
    select elements;

See here for information on IndexOfAny.

Solution 4

You could use a prefix list and then use the prefix list for comparison - this way you can easily use different prefix lists based on what range you are interested in:

 List<string> prefixList = new List<string>() { "A", "B", "C", "D", "E", "F", "G" };
 var countryAG = countryList.Where( x=> prefixList.Any( p => x.StartsWith(p)));

Solution 5

Try use this code:

var start = "a";
var end = "g";
var regex = new Regex(string.Format("^[{0}-{1}]", start, end));
var result = list.Where(x => regex.Match(x.ToLowerInvariant()).Success);

'start' and 'end' are static as an example.

Share:
17,038
Malik
Author by

Malik

Updated on August 18, 2022

Comments

  • Malik
    Malik over 1 year

    We have a list containing names of countries. We need to find names of countries from list b/w two letters. Like names of all countries with name starting b/w A-G and so on. We create following linq query but its ugly.

    var countryAG = from elements in countryList
    where elements.StartsWith("A") || 
    elements.StartsWith("B") || 
    elements.StartsWith("C") || 
    elements.StartsWith("D") || 
    elements.StartsWith("E") || 
    elements.StartsWith("F") || 
    elements.StartsWith("G") || 
    elements.StartsWith("H") 
    select elements;
    

    where countryList is created in C#

    List< string> countryList = new List< string>();
    

    Any help or any other efficient way to accomplish above task?

  • svick
    svick over 12 years
    That query won't compile, you forgot select.
  • Malik
    Malik over 12 years
    Thanks @Steven but following error during build:A query body must end with a select clause or a group clause
  • Malik
    Malik over 12 years
    @svick, where i should add select?
  • Malik
    Malik over 12 years
    Thanks @Broken but it is same as my current approach. I am not interested in writing all English alphabets.
  • Malik
    Malik over 12 years
    Thanks @Yahia but it is same as my current approach. I am not interested in writing all English alphabets.
  • Yahia
    Yahia over 12 years
    sorry to read that - you asked for a more efficient way... I would bet that the query from my answer runs faster than the one you are currently using and thus I suggested it...
  • Malik
    Malik over 12 years
    Worked like a charm. Thanks @Steven.
  • Malik
    Malik over 12 years
    Worked like a charm. Thanks @svick for all the help.
  • Malik
    Malik over 12 years
    Worked like a charm. Thanks @Yahia for your reply.
  • Malik
    Malik over 12 years
    Using regex sounds interesting. I will give this a try. Thanks for your reply.
  • svick
    svick over 12 years
    Instead of Match().Success, you can use IsMatch().
  • Malik
    Malik over 12 years
    Sorry @Yahia. I ignore efficiency aspect as I was constructing on "making things simple" and ignore "efficient" part. Sorry again. Also, do we have any way of "making things simple and efficient". Scenario is simple, we have list of countries name and we have to divide them among A-G, I-P and Q-Z. I thought Linq is quick and easy. Your thoughts please.
  • chrisaut
    chrisaut over 12 years
    Sorry, thats what I get for writing directly inside the browser :-) Thnx svick