LINQ 不以 on List开头
我有一个列表
abc
def
ghi
jkl
mno
我有另一个字符串“pq”,我需要知道列表中的每个字符串是否不以“pq”开头 - 我将如何使用 LINQ (.NET 4.0) 做到这一点?
I have a List<string> with five strings in it:
abc
def
ghi
jkl
mno
I have another string, "pq", and I need to know if each string in the list does not start with "pq" - how would I do that with LINQ (.NET 4.0)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两个选项:
任何
和全部
。您应该使用哪一个取决于您认为更具可读性:它们在效率上实际上是等效的 - 两者都会在到达以“pq”开头的元素(如果有)时立即停止。
如果您发现自己经常这样做,您甚至可以编写自己的扩展方法:
此时您将拥有:
当然,您是否发现比前两个更具可读性是个人喜好:)
Two options:
Any
andAll
. Which one you should use depends on what you find more readable:These are effectively equivalent in efficiency - both will stop as soon as they reach an element starting with "pq" if there is one.
If you find yourself doing this a lot, you could even write your own extension method:
at which point you'd have:
Whether you find that more readable than the first two is a personal preference, of course :)
这将产生 IEnumerable类型的结果。
This will produce results which is an IEnumerable<bool>