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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 赶路人之刚出发

集成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接口扩展了一大堆静态泛型方法