linq中的元素操作符

目录
  • 一、Fitst操作符
  • 二、FirstOrDefault操作符
  • 三、Last操作符
  • 四、LastOrDefault操作符
  • 五、ElementAt操作符
  • 六:ElementAtOrDefault操作符
  • 七、Single操作符
    • 1、序列包含多个元素
    • 2、序列中不包含任何元素。
  • 八、SingleOrDefault操作符
    • 1、序列包含多个元素
    • 2、序列不包含任何元素

元素操作符仅返回一个元素。

一、Fitst操作符

First操作符将返回序列中的第一个元素。如果序列中不包含任何元素,则First<T>方法将引发异常。来看看First()方法的定义:

从定义中可以看出:First()方法共有两个重载。First<T>的有参重载方法中可以指定一个条件,操作将返回序列中满足此条件的第一个元素。从查询结果上看,source.First<T>(条件)方法与source.where(条件).First<T>是一样的,但是需要注意的是:First<T>(条件)操作将返回序列中满足此条件的第一个元素,这将会忽略后面的遍历操作,效率更高。请看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            var productExp = listProduct.First();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).First();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.First(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).First();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).First(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

  • 如果序列中不包含任何元素,则First<T>方法将引发异常。

看下面的例子:

var pro = listProduct.First(p => p.CategoryId.Equals(4));
Console.WriteLine($"Id:{pro.Id},CategoryId:{pro.CategoryId},Name:{pro.Name},Price:{pro.Price},CreateTime:{pro.CreateTime}");

结果:

二、FirstOrDefault操作符

FirstOrDefault操作符也是返回序列中的第一个元素。与First()操作符不同的是:如果序列中不包含任何元素,FirstOrDefault则返回默认值,程序不会报错。来看看定义:

从定义中可以看出:FirstOrDefault和First操作符的重载方法一致。请看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            var productExp = listProduct.FirstOrDefault();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).FirstOrDefault();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.FirstOrDefault(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).FirstOrDefault();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).FirstOrDefault(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

与First()操作符不同的是:如果序列中不包含任何元素,FirstOrDefault则返回默认值,程序不会报错。看下面的例子:

// 查询listProduct中CategoryId为4的集合的第一个元素
var pro = listProduct.FirstOrDefault(p => p.CategoryId.Equals(4));
Console.WriteLine($"Id:{pro.Id},CategoryId:{pro.CategoryId},Name:{pro.Name},Price:{pro.Price},CreateTime:{pro.CreateTime}");

结果:

从上面的截图中看出:如果序列中不包含任何元素,会自动用null进行填充。但是下面输出的时候要先判断查询结果是否为null。

三、Last操作符

Last方法将返回序列中的最后一个元素。看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            var productExp = listProduct.Last();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).Last();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.Last(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).Last();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).Last(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

Last操作符和First操作符一样,如果序列中不包含任何元素,则程序会直接报错。

四、LastOrDefault操作符

LastOrDefault操作符将返回序列中的最后一个元素;如果序列中不包含任何元素,则返回默认值。来看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            var productExp = listProduct.LastOrDefault();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).LastOrDefault();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.LastOrDefault(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).LastOrDefault();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).LastOrDefault(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

LastOrDefault和FirstOrDefault一样,如果序列中不包含任何元素,则使用null值进行填充返回值。

五、ElementAt操作符

ElementAt操作符返回序列中指定索引处的元素。来看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            // 查询集合中索引为3的元素 即Id=4
            var productExp = listProduct.ElementAt(3);
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            // 查询集合中索引为2的元素 即Id=3
            var productFun = (from p in listProduct select p).ElementAt(2);
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

ElementAt()的参数值必须是集合中存在的索引,否则程序会直接报错,看下面的例子:

// 查询集合中索引为7的元素
var product = listProduct.ElementAt(7);

结果:

六:ElementAtOrDefault操作符

ElementAtOrDefault方法将返回序列中指定索引处的元素;如果索引超出范围,则返回默认值。看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // First
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 方法语法
            // 查询集合中索引为3的元素 即Id=4
            var productExp = listProduct.ElementAtOrDefault(3);
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            // 查询集合中索引为2的元素 即Id=3
            var productFun = (from p in listProduct select p).ElementAtOrDefault(2);
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

如果索引超出范围,则返回默认值。看下面的例子:

从上面的截图中看出:如果索引超出范围,则用null填充返回值。

七、Single操作符

Single操作符将从一个序列中返回单个元素,如果该序列包含多个元素,或者没有元素数为0,则会引发异常。看看定义:

从定义中能够看出:Single操作符共有两个重载的方法。无参的方法重载将从一个序列中返回单个元素,如果该序列包含多个元素,或者没有元素数为0,则会引发异常。参数为委托的方法重载将返回满足委托条件的单个元素。看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}
            };

            // 方法语法
            var productExp = listProduct.Single();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).Single();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.Single(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).Single();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).Single(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

如果序列包含多个元素,或者序列元素数为0,则会引发异常。看下面的例子。

1、序列包含多个元素

修改ListProduct为包含多个元素的集合:

// 1、序列包含多个元素
List<Product> listProduct = new List<Product>()
{
       new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
       new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
};
var productExp = listProduct.Single();

结果:

2、序列中不包含任何元素。

// 2、序列不包含任何元素
List<Product> listProduct = new List<Product>();
var productExp = listProduct.Single();

结果:

八、SingleOrDefault操作符

SingleOrDefault方操作符将从一个序列中返回单个元素。如果元素数为0,则返回默认值。看定义:

从定义中可以看出:SingleOrDefault的定义和Single的定义一致。看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElementOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> listProduct = new List<Product>()
            {
               new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}
            };

            // 方法语法
            var productExp = listProduct.SingleOrDefault();
            Console.WriteLine($"Id:{productExp.Id},CategoryId:{productExp.CategoryId},Name:{productExp.Name},Price:{productExp.Price},CreateTime:{productExp.CreateTime}");
            // 查询表达式
            var productFun = (from p in listProduct select p).Single();
            Console.WriteLine($"Id:{productFun.Id},CategoryId:{productFun.CategoryId},Name:{productFun.Name},Price:{productFun.Price},CreateTime:{productFun.CreateTime}");
            // 根据委托进行刷选
            // 查询CategoryId为1的集合的第一个元素
            // 方法语法
            var productDeleExp = listProduct.Single(p => p.CategoryId.Equals(1));
            Console.WriteLine($"Id:{productDeleExp.Id},CategoryId:{productDeleExp.CategoryId},Name:{productDeleExp.Name},Price:{productDeleExp.Price},CreateTime:{productDeleExp.CreateTime}");
            // 查询表达式
            var productDeleFun = (from p in listProduct where p.CategoryId.Equals(1) select p).Single();
            Console.WriteLine($"Id:{productDeleFun.Id},CategoryId:{productDeleFun.CategoryId},Name:{productDeleFun.Name},Price:{productDeleFun.Price},CreateTime:{productDeleFun.CreateTime}");
            // 或者使用下面的查询表达式
            var product = (from p in listProduct select p).Single(z => z.CategoryId.Equals(1));
            Console.WriteLine($"Id:{product.Id},CategoryId:{product.CategoryId},Name:{product.Name},Price:{product.Price},CreateTime:{product.CreateTime}");
            Console.ReadKey();
        }
    }
}

结果:

注意:

如果序列包含多个元素或者不包含任何元素,则用null值填充返回值。看下面例子:

1、序列包含多个元素

// 1、序列包含多个元素
List<Product> listProduct = new List<Product>()
{
       new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
       new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
};
var productExp = listProduct.SingleOrDefault();

结果:

2、序列不包含任何元素

// 2、序列不包含任何元素
List<Product> listProduct = new List<Product>();
var productExp = listProduct.SingleOrDefault();

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • linq中的连接操作符

    linq中的连接操作符主要包括Join()和GroupJoin()两个. 一.Join()操作符 Join()操作符非常类似于T-SQL中的inner join,它将两个数据源进行连接,根据两个数据源中相等的值进行匹配.例如:可以将产品表和产品类别表进行连接,得到产品名称和与其对应的类型名称.下面看看Join()方法的定义: public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEn

  • LINQ排序操作符用法

    Linq中的排序操作符包括OrderBy.OrderByDescending.ThenBy.ThenByDescending和Reverse,提供了升序或者降序排序. 一.OrderBy操作符 OrderBy操作符用于对输入序列中的元素进行排序,排序基于一个委托方法的返回值顺序.排序过程完成后,会返回一个类型为IOrderEnumerable<T>的集合对象.其中IOrderEnumerable<T>接口继承自IEnumerable<T>接口.下面来看看OrderBy的

  • linq中的串联操作符

    串联是一个将两个集合连接在一起的过程.在Linq中,这个过程通过Concat操作符实现.Concat操作符用于连接两个集合,生成一个新的集合.来看看Concat操作符的定义: public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) 从方法定义中可以看出:第二个参数为输入一个新的集合,与调用集

  • LINQ投影操作符Select与限制操作符where介绍

    一.什么是LINQ?它可以用来做什么 语言集成查询(Language Integrated Query,LINQ)是一系列标准查询操作符的集合,这些操作符几乎对每一种数据源的导航.过滤和执行操作都提供了底层的基本查询架构. LINQ可查询的数据源包括XML(可使用LINQ TO XML).关系数据(使用LINQ TO SQL,及先前的DLINQ).ADO.NET DataSet(使用LINQ TO DataSet),以及内存中的数据. 二.投影操作符:Select Select操作符对单个序列或

  • LINQ操作符SelectMany的用法

    SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: student类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SelectMany操作符 { /// <summary> /// 学生类 /// &

  • linq中的聚合操作符

    目录 一.Aggregate操作符 二.Average操作符 1.直接求基本类型集合的平均值 2.求listProduct集合中价格的平均值 三.Count操作符 四.LongCount操作符 五.Max操作符 六.Min操作符 七.Sum操作符 一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource>(this IEnumerable<TSourc

  • LINQ 标准查询操作符

    推荐大家下载本文的PDF进行阅读,可以方便的使用书签来阅读各个方法,而且代码中的关键字是高亮显示的.pdf版下载地址 http://www.jb51.net/books/24738.html 一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: 复制代码 代码如下: using (NorthwindDataContext db=new NorthwindDataContext()) { //查询

  • linq中的分组操作符

    分组是根据一个特定的值将序列中的元素进行分组.LINQ只包含一个分组操作符:GroupBy.GroupBy操作符类似于T-SQL语言中的Group By语句.来看看GroupBy的方法定义: public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey>

  • linq中的转换操作符

    这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以"To"开头的方法可枚举(即时加载)源集合并将项放入相应的集合类型. 一.AsEnumerable操作符 所有实现了IEnumerable<T>接口的类型都可以调用此方法来获取一个IEnumerable<T>集合

  • linq中的元素操作符

    目录 一.Fitst操作符 二.FirstOrDefault操作符 三.Last操作符 四.LastOrDefault操作符 五.ElementAt操作符 六:ElementAtOrDefault操作符 七.Single操作符 1.序列包含多个元素 2.序列中不包含任何元素. 八.SingleOrDefault操作符 1.序列包含多个元素 2.序列不包含任何元素 元素操作符仅返回一个元素. 一.Fitst操作符 First操作符将返回序列中的第一个元素.如果序列中不包含任何元素,则First<T

  • linq中的分区操作符

    Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作. 一.Take操作符 Take(int n)表示将从序列的开头返回数量为n的连续元素,常用于分页.其定义如下: public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count); 该方法只接受一个整数,表示要返回的结果的数量. 看下面的例子: usin

  • linq中的限定操作符

    限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LimitOperation { class Progra

  • C#使用linq计算执行元素在列表中出现次数的方法

    本文实例讲述了C#使用linq计算执行元素在列表中出现次数的方法.分享给大家供大家参考.具体如下: 这是使用linq计算元素在列表中出现的次数,调用方法非常简单,和sql语句很像 复制代码 代码如下: // Count the number of times an item appears in this list public static int CountTimes<T>(this List<T> inputList, T searchItem) { return ((fro

  • Python中遍历字典过程中更改元素导致异常的解决方法

    先来回顾一下Python中遍历字典的一些基本方法: 脚本: #!/usr/bin/python dict={"a":"apple","b":"banana","o":"orange"} print "##########dict######################" for i in dict: print "dict[%s]=" % i,

随机推荐