linq中的元素操作符

这篇文章介绍了linq中的元素操作符,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

一、Fitst操作符

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

从定义中可以看出:First()方法共有两个重载。First的有参重载方法中可以指定一个条件,操作将返回序列中满足此条件的第一个元素。从查询结果上看,source.First(条件)方法与source.where(条件).First是一样的,但是需要注意的是: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 listProduct = new List() { 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方法将引发异常。

看下面的例子:

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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List(); 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 listProduct = new List() { 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 listProduct = new List() { 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 listProduct = new List(); var productExp = listProduct.SingleOrDefault();

结果:

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

以上就是linq中的元素操作符的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程