惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - Grok.Yao

sqlserver临时启用和关闭约束 string.format 参考 大O表示法 C# 日期格式化参考 查看sql语句的执行时间及缓存执行计划 查看索引执行次数 (转)JQuery有用的50段代码 查询指定日期区间内的每一天 FF和IE兼容的捕获回车事件问题 回车自动提交Form表单的问题 将数据库记录倒为Insert语句的存储过程 IE下 JS添加Select元素的option问题 VS中的常用快捷键,可以提高开发效率 ashx中使用Session问题 关于CSS控制TD换行与否的问题解决 TFS2008 无法撤销锁定的签出 Sql获取随机时间 本地SQL脚本操作外部服务器结果集 RSA容器权限问题
C#中集合查询的问题!
Grok.Yao · 2011-03-03 · via 博客园 - Grok.Yao

在C#中使用集合时,经常会遇到集合中查询符合条件对象的问题,在一般思路上来说,都是遍历集合找出合适的对象,例如如下代码:

  foreach (Person p in lstPerson)
            {
                if (p.Name == "puma")
                {
                    //对符合条件的对象进行处理
                }
            }

其实在.net FrameWork中为我们提供了针对集合查找的方法,即Find(),FindAll(),我们来看一下怎么使用他们,

    首先我们需要定一个查找符合条件的方法,例如:

    private static bool PersonT(Person p)
        {
            if (p.Age > 10)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

  该方法接收Person对象并对Person对象的年龄判断,符合条件返回True;

下面看一下Find()方法:在MSDN中Find()定义为

public T Find (
Predicate<T> match
)

说明在Find方法中定义了一个Predicate 委托用来接收判断对象的条件,因为这个定义,所以我们创建了上面的PersonT方法,用来判断条件。
List<Person> Plist = lstPerson.FindAll(PersonT );
通过这种方式,我们就可以查找年龄在10岁以上的Person对象了!!