Lambda 表达式 - 选择运算符
我想知道如何编写 Linq(使用标准点表示法中的 lambda 表达式)查询。 我有一些名称的数组,我想根据一个语句检索一组新的名称。这是:对名称数组进行排序,并从以某个特定字母(假设是字母 M)开始的名称返回一个新列表。
这是我当前的数组:
string[] arrNames = { "Mike", "Zach", "Ella", "Allan", "Jo", "Roger", "Tito" };
我想返回这样的名字:Mike、Roger、Tito、Zach - 这 4 个; 其他 3 个名字(Allan、Ella 和 Jo 是按字母顺序排列在字母“M”下方的字母开头的名字。 这与使用运算符“StartsWith”不同。该选项仅选择以特定字母开头的名称。 我想获取从这封信开始按字母顺序排列的所有名称(因此名称从 M 到 Z 开始)。
因此,重新调整列表的名称以字母“M”或以上字母开头,按字母顺序排列。
米贾
I would like to know how to write a Linq (using lambda Expression in standard dot notation) query.
I have an array of some names, and I would like to retrevie a new array of names based on one statement. This is: Order the array of names, and return a new list from the name which starts on some specific letter (lets say letter M) on.
This is my current array:
string[] arrNames = { "Mike", "Zach", "Ella", "Allan", "Jo", "Roger", "Tito" };
I would like to return names like this: Mike, Roger, Tito, Zach - these 4;
Other 3 names (Allan, Ella and Jo are names which start with a letter that are in the alphabetica order bellow letter "M".
This is not the same as using Operator "StartsWith". This one only selects the names started on the specific letter. I would like to get all the names which are in alphabetical order from this letter and on (so names started from M to Z).
So retun list with names starts with letter "M" or above looking on the alphabetical order.
Mitja
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您需要这个:
它以默认顺序返回所有按字母顺序大于(或等于)“M”的名称(在您的情况下为
{ Mike, Zach, Roger, Tito }
)。如果您想另外排序,请使用
This gets
{ Mike, Roger, Tito, Zach }
。Looks like you need this:
which returns all the names alphabetically greater (or equal) "M", in the default order (
{ Mike, Zach, Roger, Tito }
in your case).If you want to sort it additionally, use
This gives
{ Mike, Roger, Tito, Zach }
.如果您想要不区分大小写的比较。或者使用 StringComparison.InvariantCulture 区分大小写。通常最好为字符串比较指定区域性(例如,您可以使用当前区域性或不变区域性)。
如果您的整个排序目的只是为了获取“M”之外的项目,那么您可以省略 OrderBy。
if you want case-insensitive comparisons. Or use
StringComparison.InvariantCulture
for case-sensitive. It is usually a good idea to specify the culture for string comparisons (e.g. you can use the current culture, or the invariant culture).If your whole point of sorting is just to get at the items beyond "M", then you may omit the OrderBy.