-
LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Coun
- 介绍
- ·First - 返回集合中的第一个元素;不延迟
- ·FirstOrDefault - 返回集合中的第一个元素(如果没有则返回默认值);不延迟
- ·Last - 返回集合中的最后一个元素;不延迟
- ·LastOrDefault - 返回集合中的最后一个元素(如果没有则返回默认值)
- ·ElementAt - 返回集合中指定索引的元素;不延迟
- ·ElementAtOrDefault - 返回集合中指定索引的元素(如果没有则返回默认值);不延迟
- ·Contains - 判断集合中是否包含有某一元素;不延迟
- ·Any - 判断集合中是否有元素满足某一条件;不延迟
- ·All - 判断集合中是否所有元素都满足某一条件;不延迟
- ·Count - 返回集合中的元素个数,返回int;不延迟
- ·LongCount - 返回集合中的元素个数,返回long;不延迟
- ·Sum - 集合应为数字类型集合,求其和;不延迟
- ·Min - 返回集合的最小值;不延迟
- ·Max - 返回集合的最大值;不延迟
- ·Average - 集合应为数字类型集合,求其平均值;不延迟
- ·Aggregate - 根据输入的表达式获取一个聚合值;不延迟
- ·Cast - 将集合转换为强类型集合;延迟
- ·DefaultIfEmpty - 查询结果为空则返回默认值;延迟
- ·SequenceEqual - 判断两个集合是否相同;不延迟
- ·OfType - 过滤集合中的指定类型;延迟
- ·ToArray - 将集合转换为数组;不延迟
- ·ToList - 将集合转换为List集合;不延迟
- ·ToDictionary - 将集合转换为<K, V>集合;不延迟
-
-
- 示例
- Summary3.aspx.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Xml.Linq;
-
- using System.Collections.Generic;
- using DAL;
-
- public partial class LINQ_Summary3 : System.Web.UI.Page
- {
- NorthwindDataContext _ctx = new NorthwindDataContext();
- string[] _ary = null;
-
- protected void Page_Load(object sender, EventArgs e)
- {
- _ary = new string[] { "asp.net", "csharp", "xhtml", "css", "javascript",
- "wcf", "wpf", "silverlight", "linq", "wf",
- "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs" };
-
-
-
- Summary_First_FirstOrDefault();
-
-
-
- Summary_Last_LastOrDefault();
-
-
-
- Summary_ElementAt_ElementAtOrDefault();
-
-
- Summary_Contains();
-
-
- Summary_Any();
-
-
- Summary_All();
-
-
-
- Summary_Count_LongCount();
-
-
- Summary_Sum();
-
-
- Summary_Min();
-
-
- Summary_Max();
-
-
- Summary_Average();
-
-
- Summary_Aggregate();
-
-
- Summary_Cast();
-
-
- Summary_DefaultIfEmpty();
-
-
- Summary_SequenceEqual();
-
-
- Summary_OfType();
-
-
- Summary_ToArray();
-
-
- Summary_ToList();
-
-
- Summary_ToDictionary();
- }
- }
- First - 返回集合中的第一个元素;不延迟
- FirstOrDefault - 返回集合中的第一个元素(如果没有则返回默认值);不延迟
- ///
-
-
-
- void Summary_First_FirstOrDefault()
- {
- string s = (from a in _ary
- select a).First(a => a.StartsWith("s"));
-
-
-
-
- result.InnerHtml += s + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- silverlight
-
- Last - 返回集合中的最后一个元素;不延迟
- LastOrDefault - 返回集合中的最后一个元素(如果没有则返回默认值)
- ///
-
-
-
- void Summary_Last_LastOrDefault()
- {
- string s = (from a in _ary
- select a).Last(a => a.StartsWith("s"));
-
-
-
-
- result.InnerHtml += s + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- ssrs
-
- ElementAt - 返回集合中指定索引的元素;不延迟
- ElementAtOrDefault - 返回集合中指定索引的元素(如果没有则返回默认值);不延迟
- ///
-
-
-
- void Summary_ElementAt_ElementAtOrDefault()
- {
- string s = (from a in _ary
- select a).ElementAt(3);
-
-
-
-
- result.InnerHtml += s + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- css
-
- Contains - 判断集合中是否包含有某一元素;不延迟
- ///
-
-
- void Summary_Contains()
- {
- bool b = (from a in _ary
- select a).Contains("javascript");
-
- result.InnerHtml += b.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- True
-
- Any - 判断集合中是否有元素满足某一条件;不延迟
- ///
-
-
- void Summary_Any()
- {
- bool b = (from a in _ary
- select a).Any(p => p.Length > 10);
-
- result.InnerHtml += b.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- True
-
- All - 判断集合中是否所有元素都满足某一条件;不延迟
- ///
-
-
- void Summary_All()
- {
- bool b = (from a in _ary
- select a).All(p => p.Length > 10);
-
- result.InnerHtml += b.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- False
-
- Count - 返回集合中的元素个数,返回int;不延迟
- LongCount - 返回集合中的元素个数,返回long;不延迟
- ///
-
-
-
- void Summary_Count_LongCount()
- {
- int i = (from a in _ary
- select a).Count(p => p.Length > 10);
-
-
-
- result.InnerHtml += i.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 2
-
- Sum - 集合应为数字类型集合,求其和;不延迟
- ///
-
-
- void Summary_Sum()
- {
- int i = (from a in _ary
- select a.Length).Sum();
-
- result.InnerHtml += i.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 87
-
- Min - 返回集合的最小值;不延迟
- ///
-
-
- void Summary_Min()
- {
- int i = (from a in _ary
- select a.Length).Min();
-
- result.InnerHtml += i.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 2
-
- Max - 返回集合的最大值;不延迟
- ///
-
-
- void Summary_Max()
- {
- int i = (from a in _ary
- select a.Length).Max();
-
- result.InnerHtml += i.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 12
-
- Average - 集合应为数字类型集合,求其平均值;不延迟
- ///
-
-
- void Summary_Average()
- {
- double d = (from a in _ary
- select a.Length).Average();
-
- result.InnerHtml += d.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 5.8
-
- Aggregate - 根据输入的表达式获取一个聚合值;不延迟
- ///
-
-
- void Summary_Aggregate()
- {
-
- double d = (from a in _ary
- select a.Length).Aggregate((x, y) => x + y);
-
- result.InnerHtml += d.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- 87
-
- Cast - 将集合转换为强类型集合;延迟
- ///
-
-
- void Summary_Cast()
- {
- ArrayList al = new ArrayList();
- al.Add("asp.net");
- al.Add("csharp");
- al.Add("xhtml");
-
- var list = al.Cast<string>();
-
- foreach (string s in list)
- {
- result.InnerHtml += s + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- asp.net
- csharp
- xhtml
-
- DefaultIfEmpty - 查询结果为空则返回默认值;延迟
- ///
-
-
- void Summary_DefaultIfEmpty()
- {
- var list = (from a in _ary
- where a.Length > 100
- select a).DefaultIfEmpty("xxx");
-
- foreach (string s in list)
- {
- result.InnerHtml += s + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- xxx
-
- SequenceEqual - 判断两个集合是否相同;不延迟
- ///
-
-
- void Summary_SequenceEqual()
- {
- bool b = (from a in _ary
- where a.Length > 10
- select a).SequenceEqual(from a in _ary
- where a.Length > 10
- select a);
-
- result.InnerHtml += b.ToString() + "<br />";
- result.InnerHtml += "<br />";
- }运行结果
- True
-
- OfType - 过滤集合中的指定类型;延迟
- ///
-
-
- void Summary_OfType()
- {
- object[] objects = { 1, "a", 2, "b", 3, "c" };
-
- var list = objects.OfType<string>();
-
- foreach (string s in list)
- {
- result.InnerHtml += s + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- a
- b
- c
-
- ToArray - 将集合转换为数组;不延迟
- ///
-
-
- void Summary_ToArray()
- {
- string[] ary = (from p in _ctx.Products
- where p.ProductName.Length > 30
- select p.ProductName).ToArray();
-
- foreach (string s in ary)
- {
- result.InnerHtml += s + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- Jack's New England Clam Chowder
- Louisiana Fiery Hot Pepper Sauce
- Original Frankfurter grüne So?e
- Uncle Bob's Organic Dried Pears
-
- ToList - 将集合转换为List集合;不延迟
- ///
-
-
- void Summary_ToList()
- {
- var list = (from a in _ary
- where a.Length > 10
- select a).ToList();
-
- foreach (string s in list)
- {
- result.InnerHtml += s + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- silverlight
- asp.net ajax
-
- ToDictionary - 将集合转换为<K, V>集合;不延迟
- ///
-
-
- void Summary_ToDictionary()
- {
- var dic = (from p in _ctx.Products
- where p.ProductName.Length > 30
- select p).ToDictionary(p => p.ProductID);
-
- foreach (var p in dic)
- {
- result.InnerHtml += p.Key + ":" + p.Value.ProductName + "<br />";
- }
- result.InnerHtml += "<br />";
- }运行结果
- 7:Uncle Bob's Organic Dried Pears
- 41:Jack's New England Clam Chowder
- 65:Louisiana Fiery Hot Pepper Sauce
- 77:Original Frankfurter grüne So?e
-
相关阅读:
分布式搜索引擎Elasticsearch的查询与过滤
剖析Elasticsearch集群系列第一篇 Elasticsearch的存储模型和读写操作
分布式缓存 cachecloud
npm是什么NPM的全称是Node Package Manager
Grafana监控可视化环境搭建
github ssl验证跳过
Linux分区扩容
手把手教你把Vim改装成一个IDE编程环境(图文)
根据条件批量删除document
奇智网络聊天机器人
-
原文地址:https://www.cnblogs.com/simadi/p/5153058.html
Copyright © 2020-2023
润新知