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.
Author by
Malik
Updated on August 18, 2022Comments
-
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 over 12 yearsThat query won't compile, you forgot
select
. -
Malik over 12 yearsThanks @Steven but following error during build:A query body must end with a select clause or a group clause
-
Malik over 12 years@svick, where i should add select?
-
Malik over 12 yearsThanks @Broken but it is same as my current approach. I am not interested in writing all English alphabets.
-
Malik over 12 yearsThanks @Yahia but it is same as my current approach. I am not interested in writing all English alphabets.
-
Yahia over 12 yearssorry 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 over 12 yearsWorked like a charm. Thanks @Steven.
-
Malik over 12 yearsWorked like a charm. Thanks @svick for all the help.
-
Malik over 12 yearsWorked like a charm. Thanks @Yahia for your reply.
-
Malik over 12 yearsUsing regex sounds interesting. I will give this a try. Thanks for your reply.
-
svick over 12 yearsInstead of
Match().Success
, you can useIsMatch()
. -
Malik over 12 yearsSorry @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 over 12 yearsSorry, thats what I get for writing directly inside the browser :-) Thnx svick