Linq查询-根据首个字母b/w两个范围查找字符串 [英] Linq query - find strings based upon first letter b/w two ranges

查看:186
本文介绍了Linq查询-根据首个字母b/w两个范围查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含国家名称的列表.我们需要从列表b/w两个字母中查找国家/地区的名称.类似于所有国家的名称,名称以B/w A-G开头,依此类推.我们创建以下linq查询,但它很丑.

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;

在C#中创建countryList的地方

where countryList is created in C#

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

是否有帮助或其他有效方法来完成上述任务?

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

推荐答案

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

这篇关于Linq查询-根据首个字母b/w两个范围查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆