Select method in Listlt;tgt; Collection(在 Listt 中选择方法收藏)
问题描述
我有一个 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
FindAll
和 ConvertAll
方法 - 但更惯用的现代方法是使用 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> 中选择方法收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 List<t> 中选择方法收藏
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01