linq中的元素操作符
元素操作符僅返回一個元素。
一、Fitst操作符
First操作符將返回序列中的第一個元素。如果序列中不包含任何元素,則First<T>方法將引發(fā)異常。來看看First()方法的定義:
從定義中可以看出:First()方法共有兩個重載。First<T>的有參重載方法中可以指定一個條件,操作將返回序列中滿足此條件的第一個元素。從查詢結(jié)果上看,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開發(fā)和運維", 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="高等數(shù)學", 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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
- 如果序列中不包含任何元素,則First<T>方法將引發(fā)異常。
看下面的例子:
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}");
結(jié)果:
二、FirstOrDefault操作符
FirstOrDefault操作符也是返回序列中的第一個元素。與First()操作符不同的是:如果序列中不包含任何元素,F(xiàn)irstOrDefault則返回默認值,程序不會報錯。來看看定義:
從定義中可以看出: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開發(fā)和運維", 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="高等數(shù)學", 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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
與First()操作符不同的是:如果序列中不包含任何元素,F(xiàn)irstOrDefault則返回默認值,程序不會報錯??聪旅娴睦樱?/p>
// 查詢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}");
結(jié)果:
從上面的截圖中看出:如果序列中不包含任何元素,會自動用null進行填充。但是下面輸出的時候要先判斷查詢結(jié)果是否為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開發(fā)和運維", 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="高等數(shù)學", 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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
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開發(fā)和運維", 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="高等數(shù)學", 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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
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開發(fā)和運維", 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="高等數(shù)學", 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(); } } }
結(jié)果:
注意:
ElementAt()的參數(shù)值必須是集合中存在的索引,否則程序會直接報錯,看下面的例子:
// 查詢集合中索引為7的元素 var product = listProduct.ElementAt(7);
結(jié)果:
六:ElementAtOrDefault操作符
ElementAtOrDefault方法將返回序列中指定索引處的元素;如果索引超出范圍,則返回默認值??聪旅娴睦樱?/p>
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開發(fā)和運維", 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="高等數(shù)學", 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(); } } }
結(jié)果:
注意:
如果索引超出范圍,則返回默認值??聪旅娴睦樱?/p>
從上面的截圖中看出:如果索引超出范圍,則用null填充返回值。
七、Single操作符
Single操作符將從一個序列中返回單個元素,如果該序列包含多個元素,或者沒有元素數(shù)為0,則會引發(fā)異常??纯炊x:
從定義中能夠看出:Single操作符共有兩個重載的方法。無參的方法重載將從一個序列中返回單個元素,如果該序列包含多個元素,或者沒有元素數(shù)為0,則會引發(fā)異常。參數(shù)為委托的方法重載將返回滿足委托條件的單個元素??聪旅娴睦樱?/p>
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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
如果序列包含多個元素,或者序列元素數(shù)為0,則會引發(fā)異常??聪旅娴睦?。
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開發(fā)和運維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, }; var productExp = listProduct.Single();
結(jié)果:
2、序列中不包含任何元素。
// 2、序列不包含任何元素 List<Product> listProduct = new List<Product>(); var productExp = listProduct.Single();
結(jié)果:
八、SingleOrDefault操作符
SingleOrDefault方操作符將從一個序列中返回單個元素。如果元素數(shù)為0,則返回默認值??炊x:
從定義中可以看出:SingleOrDefault的定義和Single的定義一致??聪旅娴睦樱?/p>
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}"); // 根據(jù)委托進行刷選 // 查詢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(); } } }
結(jié)果:
注意:
如果序列包含多個元素或者不包含任何元素,則用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開發(fā)和運維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, }; var productExp = listProduct.SingleOrDefault();
結(jié)果:
2、序列不包含任何元素
// 2、序列不包含任何元素 List<Product> listProduct = new List<Product>(); var productExp = listProduct.SingleOrDefault();
結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET Core MVC學習之視圖組件(View Component)
這篇文章主要給大家介紹了關于ASP.NET Core MVC學習之視圖組件(View Component)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core MVC具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08Asp.Net Core使用SignalR進行服務間調(diào)用方法示例
這篇文章主要介紹了Asp.Net Core使用SignalR進行服務間調(diào)用方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12讀寫xml所有節(jié)點個人小結(jié) 和 讀取xml節(jié)點的數(shù)據(jù)總結(jié)
讀寫xml所有節(jié)點個人小結(jié) 和 讀取xml節(jié)點的數(shù)據(jù)總結(jié)...2007-03-03