zoukankan      html  css  js  c++  java
  • Linq to objects示例

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

  • 相关阅读:
    Mac OS X下Maven的安装与配置
    [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
    IOS基础:深入理解Objective-c中@class的含义
    NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
    真机测试时的错误:No matching provisioning profiles found
    转帖Jmeter中的几个重要测试指标释义
    Spring集成log4j日志管理
    Log4J日志配置详解
    使用Redis的理由
  • 原文地址:https://www.cnblogs.com/Finding2013/p/3040655.html
Copyright © 2011-2022 走看看