C#有什么比得上Python的列表理解 [英] Does c# have anything comparable to Python's list comprehensions

查看:126
本文介绍了C#有什么比得上Python的列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成在C#中的列表。我错过Python的列表内涵。有交流#办法像列表理解或发电机语句动态创建的集合都在Python?

I want to generate a list in C#. I am missing python's list comprehensions. Is there a c# way to create collections on the fly like list comprehensions or generator statements do in python?

推荐答案

如果您使用的是C#3.0(VS2008),然后LINQ到对象可以做的非常类似的事情:

If you are using C# 3.0 (VS2008) then LINQ to Objects can do very similar things:

List<Foo> fooList = new List<Foo>();
IEnumerable<Foo> extract = from foo in fooList where foo.Bar > 10 select Foo.Name.ToUpper();

这篇关于C#有什么比得上Python的列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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