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

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - 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的那些事——从Linq扩展方法回顾C#语言基础 Linq之C#3.0语言扩展 Linq to DataSet 之Access查询 从101 LINQ Samples开始
Linq To Object实例之过滤字符集
greater · 2008-06-09 · via 博客园 - greater

目前利用Linq To Object可以查询实现了IEnumerable<T>接口的未定义类型对象数组、自定义对象数组、泛型List和Dictionary、以及string等。当然进行编程之前要添加System.Core.dll引用和命名空间System.Linq,事实上VS2008已经在建立项目的时候自动添加了。

这次要做的是一个简单的winform程序主要用Linq to object来过滤字符集,然后绑定到相应的datagrid去。

先来看看完成后的效果图(通过鼠标点击改变测试字符集):
FontFilter.jpg

本实例省略调试代码!主要Linq实现步骤简述如下:

定义系统字符集
private List<FontFamily> systemFont = new List<FontFamily>();

 1    /// <summary>
 2        /// 获取已安装的字符集
 3        /// </summary>
 4        /// <returns>字符集list</returns>

 5        private List<FontFamily> GetAllFont()
 6      {
 7
 8            foreach (FontFamily f in FontFamily.Families)
 9           {
10                systemFont.Add(new FontFamily(f.Name));
11
12            }

13            return systemFont;
14        }

1 通过查询把结果集绑定到datagrid中去
      var query
2                  = from font in systemFont
3                     where font.Name.ToLower().StartsWith(txtFontFilter.Text.ToLower())&&font.IsStyleAvailable(FontStyle.Regular)
4                     select font;
5                   //Gray Zhang给了很好的建议font.IsStyleAvailable(FontStyle.Regular),在此感谢!
6            dgvFont.DataSource = query.ToList();//这里的ToList是必须的,为了在winform中生成List进行显示. ASP.net有所不同.

可能遇到的问题: 字体"......"不支持样式"Regular",也许是由于系统字体库出了问题,更新一下.如果有更好的解决该Exception方法,请各位贴出来,谢谢!

附件