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

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Privacy International News Feed
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
博客园 - Franky
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
T
Tor Project blog
B
Blog RSS Feed
Recorded Future
Recorded Future
Scott Helme
Scott Helme
美团技术团队
V
V2EX
V
Visual Studio Blog
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
D
DataBreaches.Net
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
L
LangChain Blog
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
P
Privacy & Cybersecurity Law Blog

博客园 - greater

Matlab 7.0不断重启问题解决方法 安装Matlab过程中遇到拒绝访问怎么办? NUnit图解(一) [翻译]Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition 第二十四章 LINQ API编程(三) [翻译]Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition 第二十四章 LINQ API编程(二) [翻译]Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition 第二十四章 LINQ API编程(一) 迷人舞步 难译的英文单词 Linq 之Expression Tree再思考 VS2008代码段快捷方式小记 Linq To SQL分页失败后引发的思考 函数式编程的浅议 Linq操作符之筛选特定位置的元素 Linq与斐波那契数列共舞 Linq的那些事——从Linq扩展方法回顾C#语言基础 Linq To Object实例之过滤字符集 Linq之C#3.0语言扩展 Linq to DataSet 之Access查询 从101 LINQ Samples开始
Linq之查询非泛型集合
greater · 2008-06-19 · via 博客园 - greater

Linq to object 中我们经常可以很方便的查询多种集合比如List<T>泛型,主要的原因是他们都实现了System.Collections.Generic.IEnumerable<T>接口从而可以完成相关的查询操作但并非所有的类都实现了以上接口比如说System.Collections.ArrayList,那么通过以下三种方式可以解决。

一、OfType操作符
先看它的签名public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)根据指定类型筛选IEnumerable的元素,当source为null返回ArgumentNullException

var q = cars.OfType<Car>().Where(c=>c.Doors==2);

二、Cast强制转换
从Cast的签名就知道它可以返回IEnumerable<T> ,
public static IEnumerable<T> Cast<T>(this IEnumerable source)
但是也要注意,如果被转换的类型中不能被转为T的则会报Invalid-
CastException异常,如果是null的话则为NullReferenceException上面代码改写为:

var q = from c in cars.Cast<Car>
        
where c.Doors==2
        select 
new {c.Name}

三、明确的类型定义
定义from中的查询类型,如


var q 
= from Car c in cars
        
where c.Doors==2
        select 
new {c.Name}