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?
Try
See here for information on
IndexOfAny
.I can't test it right now, but I would try
I have two extension functions:
which creates a range of characters, and
which is just the inverse of
Contains
, mostly for readability.Together I can do:
where elements[0].In(Range('a', 'f')))
Try use this code:
'start' and 'end' are static as an example.
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:
Chars are just numbers really, thus you can compare them as such