C#中List的Find方法的使用
查找List中的某个值,可以使用循环遍历对比,查找出结果。C#中提供了Find方法,可以直接使用,只要查找条件传入就可。如下:
public class RecordInfo
{
private string recordID = string.Empty;
private string itemID = string.Empty;
private string recordAndItemID = string.Empty;
private string value = string.Empty;
public string RecordID
{
get
{
return this.recordID;
}
}
public string ItemID
{
get
{
return this.itemID;
}
}
public string RecordAndItemID
{
get
{
return this.recordAndItemID;
}
}
public string Value
{
get
{
return this.value;
}
}
public RecordInfo(string tmprecordID, string tmpitemID, string tmprecordAndItemID, string tmpvalue)
{
this.recordID = tmprecordID;
this.itemID = tmpitemID;
this.recordAndItemID = tmprecordAndItemID;
this.value = tmpvalue;
}
}
- class Program
- {
- public static void Main(string[] args)
- {
- List<User> userCollection = new List<User>();
- userCollection.Add(new User(1, "testOne"));
- userCollection.Add(new User(2, "testTwo"));
- userCollection.Add(new User(3, "testThree"));
- User resultUser = userCollection.Find(
- delegate(User user)
- {
- //return user.UserID == 0;
- return user.UserID == 1 && user.UserName.Equals("testOne");
- });
- Console.WriteLine(resultUser != null ?
- resultUser.UserID + System.Environment.NewLine + resultUser.UserName : "没有查找到");
- Console.ReadLine();
- }
- }
- public class User
- {
- private int userID = 0;
- private string userName = string.Empty;
- public int UserID
- {
- get
- {
- return this.userID;
- }
- }
- public string UserName
- {
- get
- {
- return this.userName;
- }
- }
- public User(int userID, string userName)
- {
- this.userID = userID;
- this.userName = userName;
- }
- }
C#中List的Find方法的使用的更多相关文章
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- Android中锁定文件的方法
androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...
- jQuery中的事件绑定方法
在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...
- Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...
- Power BI官方视频(2) Power BI嵌入到应用中的3种方法
今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...
- JQuery中each()的使用方法说明
JQuery中each()的使用方法说明 对于jQuery对象,只是把each方法简单的进行了委托:把jQuery对象作为第一个参数传递给jQuery的each方法.换句话说:jQuery提供的eac ...
- Dedecms去掉URL中a目录的方法
本文实例讲述了Dedecms去掉URL中a目录的方法.分享给大家,供大家参考.具体分析如下: 使用dedecms的朋友可能会发现自己的URL目录生成是会自动带有一个/A/目录了,那么要如何去掉URL中 ...
- 【转】C#中WinForm程序退出方法技巧总结
C#中WinForm程序退出方法技巧总结 一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.Ex ...
- inux中shell截取字符串方法总结
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...
- 创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。
创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法). ackage com.chuoji.text01; pub ...
随机推荐
- Linux How to add a new disk to LVM
转自:http://blog.itpub.net/7191998/viewspace-772060/ 1.check old diskspace and device listdf -halfree ...
- ExtJs学习笔记之Window组件
Window窗体组件 window是一个指定的打算作为一个应用程序窗口的面板,默认窗口是浮动的,resizable, 并且draggable,默认的,窗体靠document.body呈现. 1.示例: ...
- AR增强现实特点、关键技术和应用
http://wenku.baidu.com/link?url=ABXxm5yezMIQRJUV7XvNWUe_QpUUdpQ3IxGRpYUa760iex1_bygCcTBvEhCMvrdLAmSX ...
- Jfinal中定时器的初步探索(二)
第一篇中增加的是程序代码的实现,本篇我们将通过配置文件进行定时器的配置,减少代码量,提高灵活性. 1.需要用到的文件:quartz.properties,据说这个文件如果没有的话,按默认的走,结果布署 ...
- openstack(liberty): 简单网络连接图
openstack起初的网络部分是和计算核心nova合在一起的,后来被拆分出来,独立成为一个模块, 现在名为Neutron. 本博文是学习记录,记录的是基于GRE tunnel技术的neutron和计 ...
- Crypto库实现PKCS7签名与签名验证
在windows中,可以直接使用微软提供的crypto库实现PKCS7签名与签名验证.签名接口函数为CryptSignMessage,其接口定义为: BOOL WINAPI CryptSignMess ...
- jsonp get 和 post
原文地址:http://blog.sina.com.cn/s/blog_4a7e719d0100zqzh.html jsonp获取服务器的数据,有两种一,跨域二,不跨域如果跨域js的写法有两种1,&l ...
- Python try/except/finally应用
1.通过if和else处理异常 import os if os.path.exists('sketch.txt'): data = open ('sketch.txt') for each_line ...
- .NET简谈插件系统开发模式
摘选自[王清培]博客 http://www.cnblogs.com/wangiqngpei557/archive/2011/06/10/2077413.html 今天跟大家分享一下我们在日常开发中并不 ...
- CXF发布restful WebService的入门例子(服务器端)
研究了两天CXF对restful的支持. 现在,想实现一个以 http://localhost:9999/roomservice 为入口, http://localhost:9999/roomse ...