查找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;
}
}

  1. class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. List<User> userCollection = new List<User>();
  6. userCollection.Add(new User(1, "testOne"));
  7. userCollection.Add(new User(2, "testTwo"));
  8. userCollection.Add(new User(3, "testThree"));
  9. User resultUser = userCollection.Find(
  10. delegate(User user)
  11. {
  12. //return user.UserID == 0;
  13. return user.UserID == 1 && user.UserName.Equals("testOne");
  14. });
  15. Console.WriteLine(resultUser != null ?
  16. resultUser.UserID + System.Environment.NewLine + resultUser.UserName : "没有查找到");
  17. Console.ReadLine();
  18. }
  19. }
  20. public class User
  21. {
  22. private int userID = 0;
  23. private string userName = string.Empty;
  24. public int UserID
  25. {
  26. get
  27. {
  28. return this.userID;
  29. }
  30. }
  31. public string UserName
  32. {
  33. get
  34. {
  35. return this.userName;
  36. }
  37. }
  38. public User(int userID, string userName)
  39. {
  40. this.userID = userID;
  41. this.userName = userName;
  42. }
  43. }

C#中List的Find方法的使用的更多相关文章

  1. JavaScript中Math对象的方法介绍

    1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...

  2. Android中锁定文件的方法

    androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...

  3. jQuery中的事件绑定方法

    在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...

  4. Eclipse中自动提示的方法参数都是arg0,arg1的解决方法

    Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...

  5. Power BI官方视频(2) Power BI嵌入到应用中的3种方法

    今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...

  6. JQuery中each()的使用方法说明

    JQuery中each()的使用方法说明 对于jQuery对象,只是把each方法简单的进行了委托:把jQuery对象作为第一个参数传递给jQuery的each方法.换句话说:jQuery提供的eac ...

  7. Dedecms去掉URL中a目录的方法

    本文实例讲述了Dedecms去掉URL中a目录的方法.分享给大家,供大家参考.具体分析如下: 使用dedecms的朋友可能会发现自己的URL目录生成是会自动带有一个/A/目录了,那么要如何去掉URL中 ...

  8. 【转】C#中WinForm程序退出方法技巧总结

    C#中WinForm程序退出方法技巧总结 一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.Ex ...

  9. inux中shell截取字符串方法总结

    shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  10. 创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。

    创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法). ackage com.chuoji.text01; pub ...

随机推荐

  1. poj 1597 Uniform Generator【生成指定范围内所有随机数】

    本文参考资料:http://hi.baidu.com/bnjyjncwbdbjnzr/item/1f997cfdd225d5d143c36a58 题意:一个生成随机数的函数, Seed[x+1] =  ...

  2. angular.element方法汇总以及AngularJS 动态添加元素和删除元素

    addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...

  3. myBatis抛出异常Result Maps collection already contains value ...

    BaseResultMap 是自动生成的,非说已经包含了,NM! 删除tomcat下的 D:\apache-tomcat-7.0.52\webapps\chp-approve\WEB-INF\clas ...

  4. 基于HDInsight 3.4 HBase集群规划参考

    基于linux 创建HDInsight HBase集群,选择最小配置,zk(3).NN(2).WN(2),集群节点默认组件服务规划如下 NN0: Active NameNode /HDFS ZKFai ...

  5. hadoop(四):配置参数

    hadoop参数配置,主要是配置 core-site.xml,hdfs-site.xml,mapred-site.xml 三个配置文件,core-site.xml是全局配置,hdfs-site.xml ...

  6. ie数组不支持indexOf 方法解决

    if(!Array.prototype.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0;i<this.length; ...

  7. 如何将NetBeans转换为英文版(图)

    很郁闷,又是一个系统语言的问题. NetBeans下载下来之后,怎么装都是个中文版,或者半中文半英文,不伦不类的.上网找了一个圈,很多都讲得不清楚,最后终于有了一个答案: 在NetBeans的配置文件 ...

  8. IntelliJ IDEA安装AngularJS插件

    Settings→Plugins→Install JetBrains plugins... 输入an后选择AngularJS,然后点击Install进行安装 按照提示重启IDEA 下图为插件管理窗口, ...

  9. erlang实现ssh

    借鉴自http://blog.csdn.net/the__one/article/details/6567443和http://blog.yufeng.info/archives/1509 单命令启动 ...

  10. 在Linux中使用VS Code编译调试C++项目

    最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS Code在Linux下的安装这里就不提了,不管是CentO ...