博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault
阅读量:6277 次
发布时间:2019-06-22

本文共 3822 字,大约阅读时间需要 12 分钟。

Sum - 集合应为数字类型集合,求其和;不延迟
/// <summary>
InBlock.gif        
/// Sum - 集合应为数字类型集合,求其和;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Sum()
InBlock.gif        {
InBlock.gif                
int i = (from a
in _ary
InBlock.gif                                 select a.Length).Sum();
InBlock.gif
InBlock.gif                result.InnerHtml += i.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
87
Min - 返回集合的最小值;不延迟
/// <summary>
InBlock.gif        
/// Min - 返回集合的最小值;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Min()
InBlock.gif        {
InBlock.gif                
int i = (from a
in _ary
InBlock.gif                                 select a.Length).Min();
InBlock.gif
InBlock.gif                result.InnerHtml += i.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
2
Max - 返回集合的最大值;不延迟
/// <summary>
InBlock.gif        
/// Max - 返回集合的最大值;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Max()
InBlock.gif        {
InBlock.gif                
int i = (from a
in _ary
InBlock.gif                                 select a.Length).Max();
InBlock.gif
InBlock.gif                result.InnerHtml += i.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
12
Average - 集合应为数字类型集合,求其平均值;不延迟
/// <summary>
InBlock.gif        
/// Average - 集合应为数字类型集合,求其平均值;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Average()
InBlock.gif        {
InBlock.gif                
double d = (from a
in _ary
InBlock.gif                                        select a.Length).Average();
InBlock.gif
InBlock.gif                result.InnerHtml += d.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
5.8
Aggregate - 根据输入的表达式获取一个聚合值;不延迟
/// <summary>
InBlock.gif        
/// Aggregate - 根据输入的表达式获取一个聚合值;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Aggregate()
InBlock.gif        {
InBlock.gif                
// 以下算法的Aggregate相当于Sum
InBlock.gif                
double d = (from a
in _ary
InBlock.gif                                        select a.Length).Aggregate((x, y) => x + y);
InBlock.gif
InBlock.gif                result.InnerHtml += d.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
87
Cast - 将集合转换为强类型集合;延迟
/// <summary>
InBlock.gif        
/// Cast - 将集合转换为强类型集合;延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_Cast()
InBlock.gif        {
InBlock.gif                ArrayList al =
new ArrayList();
InBlock.gif                al.Add(
"asp.net");
InBlock.gif                al.Add(
"csharp");
InBlock.gif                al.Add(
"xhtml");
InBlock.gif
InBlock.gif                var list = al.Cast<
string>();
InBlock.gif
InBlock.gif                
foreach (
string s
in list)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += s +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
asp.net
csharp
xhtml
DefaultIfEmpty - 查询结果为空则返回默认值;延迟
/// <summary>
InBlock.gif        
/// DefaultIfEmpty - 查询结果为空则返回默认值;延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_DefaultIfEmpty()
InBlock.gif        {
InBlock.gif                var list = (from a
in _ary
InBlock.gif                                        where a.Length > 100
InBlock.gif                                        select a).DefaultIfEmpty(
"xxx");
InBlock.gif
InBlock.gif                
foreach (
string s
in list)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += s +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
xxx
SequenceEqual - 判断两个集合是否相同;不延迟
/// <summary>
InBlock.gif        
/// SequenceEqual - 判断两个集合是否相同;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_SequenceEqual()
InBlock.gif        {
InBlock.gif                
bool b = (from a
in _ary
InBlock.gif                                    where a.Length > 10
InBlock.gif                                    select a).SequenceEqual(from a
in _ary
InBlock.gif                                                                                    where a.Length > 10
InBlock.gif                                                                                    select a);
InBlock.gif
InBlock.gif                result.InnerHtml += b.ToString() +
"<br />";
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
True
OfType - 过滤集合中的指定类型;延迟
InBlock.gif
/// <summary>
InBlock.gif        
/// OfType - 过滤集合中的指定类型;延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_OfType()
InBlock.gif        {
InBlock.gif                
object[] objects = { 1,
"a", 2,
"b", 3,
"c" };
InBlock.gif
InBlock.gif                var list = objects.OfType<
string>();
InBlock.gif
InBlock.gif                
foreach (
string s
in list)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += s +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
a
b
c
ToArray - 将集合转换为数组;不延迟
/// <summary>
InBlock.gif        
/// ToArray - 将集合转换为数组;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_ToArray()
InBlock.gif        {
InBlock.gif                
string[] ary = (from p
in _ctx.Products
InBlock.gif                                                where p.ProductName.Length > 30
InBlock.gif                                                select p.ProductName).ToArray();
InBlock.gif
InBlock.gif                
foreach (
string s
in ary)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += s +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
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<T>集合;不延迟
/// <summary>
InBlock.gif        
/// ToList - 将集合转换为List<T>集合;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_ToList()
InBlock.gif        {
InBlock.gif                var list = (from a
in _ary
InBlock.gif                                        where a.Length > 10
InBlock.gif                                        select a).ToList();
InBlock.gif
InBlock.gif                
foreach (
string s
in list)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += s +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
silverlight
asp.net ajax
ToDictionary - 将集合转换为<K, V>集合;不延迟
InBlock.gif
/// <summary>
InBlock.gif        
/// ToDictionary - 将集合转换为<K, V>集合;不延迟
InBlock.gif        
/// </summary>
InBlock.gif        
void Summary_ToDictionary()
InBlock.gif        {
InBlock.gif                var dic = (from p
in _ctx.Products
InBlock.gif                                     where p.ProductName.Length > 30
InBlock.gif                                     select p).ToDictionary(p => p.ProductID);
InBlock.gif
InBlock.gif                
foreach (var p
in dic)
InBlock.gif                {
InBlock.gif                        result.InnerHtml += p.Key +
":" + p.Value.ProductName +
"<br />";
InBlock.gif                }
InBlock.gif                result.InnerHtml +=
"<br />";
InBlock.gif        }
 
运行结果
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
OK

本文出自 “” 博客,请务必保留此出处

转载地址:http://feyva.baihongyu.com/

你可能感兴趣的文章
高德开放平台开放源代码 鼓励开发者创新
查看>>
《高并发Oracle数据库系统的架构与设计》一2.5 索引维护
查看>>
《Exchange Server 2010 SP1/SP2管理实践》——2.4 部署外部网络环境
查看>>
Firefox 是 Pwn2own 2014 上攻陷次数最多的浏览器
查看>>
阿里感悟(十八)- 应届生Review
查看>>
《计算广告:互联网商业变现的市场与技术》一第一部分 在线广告市场与背景...
查看>>
话说模式匹配(5) for表达式中的模式匹配
查看>>
《锋利的SQL(第2版)》——1.7 常用函数
查看>>
《Arduino家居安全系统构建实战》——1.5 介绍用于机器学习的F
查看>>
jquery中hover()的用法。简单粗暴
查看>>
线程管理(六)等待线程的终结
查看>>
spring boot集成mongodb最简单版
查看>>
DELL EqualLogic PS存储数据恢复全过程整理
查看>>
《Node.js入门经典》一2.3 安装模块
查看>>
《Java 开发从入门到精通》—— 2.5 技术解惑
查看>>
Linux 性能诊断 perf使用指南
查看>>
实操分享:看看小白我如何第一次搭建阿里云windows服务器(Tomcat+Mysql)
查看>>
Sphinx 配置文件说明
查看>>
数据结构实践——顺序表应用
查看>>
python2.7 之centos7 安装 pip, Scrapy
查看>>