Sum - 集合应为数字类型集合,求其和;不延迟
/// <summary> /// Sum - 集合应为数字类型集合,求其和;不延迟 /// </summary> void Summary_Sum() { int i = (from a in _ary select a.Length).Sum(); result.InnerHtml += i.ToString() + "<br />"; result.InnerHtml += "<br />"; }
运行结果87Min - 返回集合的最小值;不延迟
/// <summary> /// Min - 返回集合的最小值;不延迟 /// </summary> void Summary_Min() { int i = (from a in _ary select a.Length).Min(); result.InnerHtml += i.ToString() + "<br />"; result.InnerHtml += "<br />"; }
运行结果2Max - 返回集合的最大值;不延迟
/// <summary> /// Max - 返回集合的最大值;不延迟 /// </summary> void Summary_Max() { int i = (from a in _ary select a.Length).Max(); result.InnerHtml += i.ToString() + "<br />"; result.InnerHtml += "<br />"; }
运行结果12Average - 集合应为数字类型集合,求其平均值;不延迟
/// <summary> /// Average - 集合应为数字类型集合,求其平均值;不延迟 /// </summary> void Summary_Average() { double d = (from a in _ary select a.Length).Average(); result.InnerHtml += d.ToString() + "<br />"; result.InnerHtml += "<br />"; }
运行结果5.8Aggregate - 根据输入的表达式获取一个聚合值;不延迟
/// <summary> /// Aggregate - 根据输入的表达式获取一个聚合值;不延迟 /// </summary> void Summary_Aggregate() { // 以下算法的Aggregate相当于Sum double d = (from a in _ary select a.Length).Aggregate((x, y) => x + y); result.InnerHtml += d.ToString() + "<br />"; result.InnerHtml += "<br />"; }
运行结果87Cast - 将集合转换为强类型集合;延迟
/// <summary> /// Cast - 将集合转换为强类型集合;延迟 /// </summary> 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.netcsharpxhtmlDefaultIfEmpty - 查询结果为空则返回默认值;延迟
/// <summary> /// DefaultIfEmpty - 查询结果为空则返回默认值;延迟 /// </summary> 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 />"; }
运行结果xxxSequenceEqual - 判断两个集合是否相同;不延迟
/// <summary> /// SequenceEqual - 判断两个集合是否相同;不延迟 /// </summary> 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 />"; }
运行结果TrueOfType - 过滤集合中的指定类型;延迟
/// <summary> /// OfType - 过滤集合中的指定类型;延迟 /// </summary> 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 />"; }
运行结果abcToArray - 将集合转换为数组;不延迟
/// <summary> /// ToArray - 将集合转换为数组;不延迟 /// </summary> 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 ChowderLouisiana Fiery Hot Pepper SauceOriginal Frankfurter grüne So?eUncle Bob's Organic Dried PearsToList - 将集合转换为List<T>集合;不延迟
/// <summary> /// ToList - 将集合转换为List<T>集合;不延迟 /// </summary> 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 />"; }
运行结果silverlightasp.net ajaxToDictionary - 将集合转换为<K, V>集合;不延迟
/// <summary> /// ToDictionary - 将集合转换为<K, V>集合;不延迟 /// </summary> 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 Pears41:Jack's New England Clam Chowder65:Louisiana Fiery Hot Pepper Sauce77:Original Frankfurter grüne So?eOK
本文出自 “” 博客,请务必保留此出处