List中选择方法< t>采集 [英] Select method in List<t> Collection
问题描述
datarow [] drow = dataset.datatable.select(searchcriteria)
。但是在列表集合中,没有可用于查找特定值的方法。有什么办法可以根据我的搜索条件选择一些值吗?我想知道这是否可行。请帮助我。嗯,从列表< T>
具有 FindAll
和 ConvertAll
方法 - 但更惯用的现代方法是使用LINQ:
//查找所有30岁以上的人
var query1 = list.Where(person => person.Age> 30);
//查找每个人的名字
var query2 = list.Select(person => person.Name);
您需要在文件中使用using指令才能使其发挥作用:
使用System.Linq;
请注意,这些不使用字符串来表达谓词和项目 - 他们使用代理,通常从lambda表达式如上所述。
如果lambda表达式和LINQ是新的,我建议您先获取一本涵盖LINQ的书,例如 LINQ in Action , Pro LINQ , C#4 in a Nutshell or my拥有深度C#。您肯定可以从网络教程中学习LINQ,但我认为这是一项重要的技术,值得花时间彻底学习。
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.
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);
You'll need a using directive in your file to make this work:
using System.Linq;
Note that these don't use strings to express predicates and projects - they use delegates, usually created from lambda expressions as above.
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屋!