.net 流氓
天下无难事,有志者成之;天下无易事,有恒者得之。
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 />";
        }
 
运行结果
87

Min - 返回集合的最小值;不延迟
/// <summary>
        /// Min - 返回集合的最小值;不延迟
        /// </summary>
        void Summary_Min()
        {
                int i = (from a in _ary
                                 select a.Length).Min();

                result.InnerHtml += i.ToString() + "<br />";
                result.InnerHtml += "<br />";
        }
 
运行结果
2

Max - 返回集合的最大值;不延迟
/// <summary>
        /// Max - 返回集合的最大值;不延迟
        /// </summary>
        void Summary_Max()
        {
                int i = (from a in _ary
                                 select a.Length).Max();

                result.InnerHtml += i.ToString() + "<br />";
                result.InnerHtml += "<br />";
        }
 
运行结果
12

Average - 集合应为数字类型集合,求其平均值;不延迟
/// <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.8

Aggregate - 根据输入的表达式获取一个聚合值;不延迟
/// <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 />";
        }
 
运行结果
87

Cast - 将集合转换为强类型集合;延迟
/// <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.net
csharp
xhtml

DefaultIfEmpty - 查询结果为空则返回默认值;延迟
/// <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 />";
        }
 
运行结果
xxx

SequenceEqual - 判断两个集合是否相同;不延迟
/// <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 />";
        }
 
运行结果
True

OfType - 过滤集合中的指定类型;延迟
/// <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 />";
        }
 
运行结果
a
b
c

ToArray - 将集合转换为数组;不延迟
/// <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 Chowder
Louisiana Fiery Hot Pepper Sauce
Original Frankfurter grüne So?e
Uncle Bob's Organic Dried Pears

ToList - 将集合转换为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 />";
        }
 
运行结果
silverlight
asp.net ajax

ToDictionary - 将集合转换为<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 Pears
41:Jack's New England Clam Chowder
65:Louisiana Fiery Hot Pepper Sauce
77:Original Frankfurter grüne So?e


OK
[源码下载]

本文出自 “webabcd” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/345003

posted on 2011-08-03 20:35  .net 流氓  阅读(507)  评论(0编辑  收藏  举报