在 List<t> 中选择方法收藏 [英] Select method in List&lt;t&gt; Collection

查看:17
本文介绍了在 List<t> 中选择方法收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp.net 应用程序,现在我正在使用数据集进行数据操作.我最近开始将此数据集转换为 List 集合.但是,在某些地方它不起作用.一是在我的旧版本中,我使用的是 datarow[] drow = dataset.datatable.select(searchcriteria).但在 List 集合中,没有可用于查找特定值的方法.我有什么办法可以根据我的搜索条件选择一些值吗?我想知道这是否可能.请帮帮我.

I have an asp.net application, and now I am using datasets for data manipulation. I recently started to convert this dataset to a List collection. But, in some places it doesn't work. One is that in my old version I am using datarow[] drow = dataset.datatable.select(searchcriteria). But in the List collection there is no method available for finding particular values. Is there any way for me to select some values according with my search criteria? I want to know if this is possible. Please help me.

推荐答案

好吧,从 List 确实FindAllConvertAll 方法 - 但更惯用的现代方法是使用 LINQ:

Well, to start with List<T> does have the FindAll and ConvertAll methods - but the more idiomatic, modern approach is to use LINQ:

// Find all the people older than 30
var query1 = list.Where(person => person.Age > 30);

// Find each person's name
var query2 = list.Select(person => person.Name);

您需要在文件中使用 using 指令来完成这项工作:

You'll need a using directive in your file to make this work:

using System.Linq;

请注意,这些不使用字符串来表示谓词和项目 - 它们使用委托,通常如上所述从 lambda 表达式创建.

Note that these don't use strings to express predicates and projects - they use delegates, usually created from lambda expressions as above.

如果 lambda 表达式和 LINQ 对您来说是新的,我建议您先买一本介绍 LINQ 的书,例如 LINQ in Action, Pro LINQ, C# 4 中Nutshell 或我自己的C# 深度.您当然可以仅从网络教程中学习 LINQ,但我认为它是如此重要的技术,值得花时间彻底学习它.

If lambda expressions and LINQ are new to you, I would suggest you get a book covering LINQ first, such as LINQ in Action, Pro LINQ, C# 4 in a Nutshell or my own C# in Depth. You certainly can learn LINQ just from web tutorials, but I think it's such an important technology, it's worth taking the time to learn it thoroughly.

这篇关于在 List<t> 中选择方法收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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