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

  • 相关阅读:
    torch.optim.SGD()各参数的解释
    pytorch中y.data.norm()的含义
    sklearn分类模块
    python处理nii文件
    cvpr2019_Unsupervised Person Re-identification by Soft Multilabel Learning
    attention机制
    contrastive loss
    pytorch扩展——如何自定义前向和后向传播
    python | 实现多行向量(matrix)两两计算余弦距离、欧几里德距离
    判定是否过拟合、欠拟合的一种方式
  • 原文地址:https://www.cnblogs.com/Finding2013/p/3040655.html
Copyright © 2011-2022 走看看