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

推荐订阅源

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

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast yield return 和 Func Lamda表达式 IDisposable 匿名类型与扩展方法 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
Linq to objects示例
赶路人之刚出发 · 2013-04-24 · via 博客园 - 赶路人之刚出发

与linq to sql类似,所有继承了IEnumerable的类型均可使用LINQ,如下string[]数组基类为Array,而Array实现了IEnumerable,所以也可使用linq

static void Main(string[] args)
        {
            string[] myWords = new string[] { "what", "is", "your", "name", "my", "name", "is" };
            var obj = from m in myWords
                      group m by m.Length into lengthgroups
                      orderby lengthgroups.Key
                      select new
                      {
                          length = lengthgroups.Key,
                          wordCollection = lengthgroups
                      };
            foreach (var item in obj)
            {
                Console.WriteLine("包含" + item.length + "个字符的单词有:");
                foreach (string stra in item.wordCollection)
                {
                    Console.WriteLine(stra);
                }
            }

 另一种写法:

 static void Main(string[] args)
        {
            string[] myWords = new string[] { "what", "is", "your", "name", "my", "name", "is" };
            var groups = myWords.GroupBy(o => o.Length);
            foreach (var item in groups)
            {
                Console.WriteLine("包含" + item.Key + "个字符的单词有:");
                foreach (string stra in item)
                {
                    Console.WriteLine(stra);
                }
            }

或者:

 static void Main(string[] args)
        {
            string[] myWords = new string[] { "what", "is", "your", "name", "my", "name", "is" };
            var groups = myWords.GroupBy(o => o.Length);
            foreach (IGrouping<int,string> item in groups)
            {
                Console.WriteLine("包含" + item.Key + "个字符的单词有:");
                foreach (string stra in item)
                {
                    Console.WriteLine(stra);
                }
            }

 获取序列中元素位置:

string[] myWords = new string[] { "what", "is", "your", "name", "my", "name", "is" };
            var words = myWords.Select((word, index) => { return new { index, word.Length, word, title = "aaa" }; }).OrderBy(o => o.Length);

Select方法有两种定义,这里用的是第一种,可以接受的是一个TSource和一个int类型的参数返回为自定义匿名类型的匿名方法

 //
        // 摘要:
        //     通过合并元素的索引将序列的每个元素投影到新表中。
        //
        // 参数:
        //   source:
        //     一个值序列,要对该序列调用转换函数。
        //
        //   selector:
        //     一个应用于每个源元素的转换函数;函数的第二个参数表示源元素的索引。
        //
        // 类型参数:
        //   TSource:
        //     source 中的元素的类型。
        //
        //   TResult:
        //     selector 返回的值的类型。
        //
        // 返回结果:
        //     一个 System.Collections.Generic.IEnumerable<T>,其元素为对 source 的每个元素调用转换函数的结果。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     source 或 selector 为 null。
        public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);
        //
        // 摘要:
        //     将序列中的每个元素投影到新表中。
        //
        // 参数:
        //   source:
        //     一个值序列,要对该序列调用转换函数。
        //
        //   selector:
        //     应用于每个元素的转换函数。
        //
        // 类型参数:
        //   TSource:
        //     source 中的元素的类型。
        //
        //   TResult:
        //     selector 返回的值的类型。
        //
        // 返回结果:
        //     一个 System.Collections.Generic.IEnumerable<T>,其元素为对 source 的每个元素调用转换函数的结果。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     source 或 selector 为 null。
        public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

其实LINQ本质就是为IEnumerable接口扩展了一大堆静态泛型方法