博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
System.Linq
阅读量:5264 次
发布时间:2019-06-14

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

class Program

    {
        static void Main( string[ ] args )
        {
            SumEx1( );
        }

        class package

        {
            public string Company { get; set; }
            public double weight { get; set; }
        }
        public static void SumEx1( )
        {
            List<package> pack = new List<package>
            { new package {Company = "aaa",weight=11.00},
              new package {Company = "bbb",weight=22.00},
              new package { Company = "Wingtip Toys", weight = 33.00 },
              new package { Company = "Adventure Works", weight = 44.00 } };

            //string.Sum()方法,合计数字类型总和。

            double totalWeight = pack.Sum( p => p.weight );
            Console.WriteLine( totalWeight);

            // Contains() 方法,是否在子字符串里出现过,为true,则只显示此字符串。

            IEnumerable<package> var = from num in pack where num.Company.Contains("aaa") select num;

            foreach ( package item in var )

            {
                Console.WriteLine( item.Company);
                Console.WriteLine( item.weight );
            }
        }
    }

namespace Console_linq2

{
    class Program
    {
        struct MyStruct
        {
            public string name;
            public int age;
            private string p;
            public MyStruct( string p )
            {
                this.p = "a";
                name = "张三";
                age = 21;
            }
        }
        static void Main( string[ ] args )
        {
            stringQueryFunction( );
            IntQueryFunction( );
            LinqToSqlFunction( );
            StructFunction( );
            ArrayReverse( );
            ListReverse( );
            ToListFunction( );
        }
        private static void ToListFunction( )
        {
            //int[ ] age = new int[ ] { 34, 54, 76, 57, 54, 6, 3, 53 };
            string[ ] fruits = { "apple", "passionfruit", "banana", "mango",
                      "orange", "blueberry", "grape", "strawberry" };
            Console.WriteLine( "ToList()之前:" );
            foreach ( string item in fruits )
            {
                Console.WriteLine( item);
            }
            Console.WriteLine( "ToList()之后aaaa:" );
            
            //ToList()方法,位数组的每个元素计算个数
            List<int> lengths = fruits.Select( fruit => fruit.Length ).ToList( );
            foreach ( int item in lengths )
            {
                Console.WriteLine( item);
            }
            Console.WriteLine( );
        }
        private static void ListReverse( )
        {
            Console.WriteLine( "LastOrDefault( )的使用:" );
            //LastOrDefault( ) 返回列表中的最后一个元素
            string[ ] fruits = { "aa", "bb", "cc", "dd" };
            string last = fruits.LastOrDefault( );
            Console.WriteLine(
                String.IsNullOrEmpty( last ) ? "<string is null or empty>" : last );
            /*
             This code produces the following output:
             <string is null or empty>
            */
            Console.WriteLine( "List<int>泛型倒序:");
            List<int> list = new List<int>( );
            list.Add( 23 );
            list.Add( 4 );
            list.Add( 34 );
            list.Add( 8 );
            list.Add( 3 );
            list.Add( 5 );
            list.IndexOf( 34 );
           
            Console.WriteLine( "排序之前:");
            for ( int i = 0; i < list.Count; i++ )
            {
                Console.WriteLine( list[i]);
            }
            list.Sort( );
            Console.WriteLine( "排序之后:");
            foreach ( int item in list )
            {
                Console.WriteLine( item );
            }
            Console.WriteLine( );
            Console.WriteLine( "倒序之后:");
           
            list.Reverse( );
            foreach ( int item in list )
            {
                Console.WriteLine( item);
            }
        }
        private static void ArrayReverse( )
        {
            int[ ] inta = { 34, 6, 58, 53, 6 };
            Console.WriteLine( "给inta[]排序");
            for ( int i = 0; i < inta.Length; i++ )
            {
                for ( int j = 0; j < inta.Length-i-1; j++ )
                {
                    if ( inta[j] < inta[j+1] )
                    {
                        int tmp = inta[ j + 1 ];
                        inta[ j + 1 ] = inta[ j ];
                        inta[ j ] = tmp;
                    }
                }
                Console.WriteLine( inta[i]);
            }
        }
        private static void StructFunction( )
        {
            MyStruct m = new MyStruct( );
            string[] message = { "aaa", "ddd" };
            
            Console.WriteLine( " IEnumerable<MyStruct>:" );
            IEnumerable<MyStruct> query = from c in message where c != null orderby c select new MyStruct
                ( m.name = c.ToString() );
            foreach ( var item in query )
            {
                Console.WriteLine( item.age+"\t"+ item.name );
           
            }
        }
        private static void LinqToSqlFunction( )
        {
            
            List<double> quantity = new List<double> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            List<double> quantity1 = new List<double> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            List<string> str = new List<string> { "a", "c", "d", "e", "f", "g", "h" };
            List<string> str1 = new List<string> { "a", "c", "d", "e", "f", "g", "h" };
            var query = from q in quantity
                        select q;
            Console.WriteLine( "Avg:" );
            Console.WriteLine( query.Average( ) );
            Console.WriteLine( "count:" );
            Console.WriteLine( query.LongCount( ) );//计算元素总量
            Console.WriteLine( "int64count" );
            Console.WriteLine( query.Count( ) );//计算元素总量
            Console.WriteLine( "Max:" );
            Console.WriteLine( query.Max( ) );//计算元素最大值
            Console.WriteLine( "Min:" );
            Console.WriteLine( query.Min( ) );//计算元素最小值
            Console.WriteLine( "sum:" );
            Console.WriteLine( query.Sum( ) );//计算元素总和
            double average = quantity.Average( );//计算平均值
            Console.WriteLine( "avg:" );
            Console.WriteLine( average );
            IEnumerable<double> avl = quantity.Distinct( );
            Console.WriteLine( "List:" );
            foreach ( double item in avl )
            {
                Console.WriteLine( item );
            }
            Console.WriteLine( );
            Console.WriteLine( "将两个集合通过 Union 方法联合之后显示,Union方法只显示不重复的数据:" );
            var inion2 = str.Union( str1 );
            foreach ( var item in inion2 )
            {
                Console.Write(item+" " );
            }
            Console.WriteLine( );
            IEnumerable<double> union = quantity.Union( quantity1 );
            foreach ( double item in union )
            {
                Console.Write( item + " " );
            }
            Console.WriteLine( );
            Console.WriteLine( "Intersect【出现多少个重复】:" );
            IEnumerable<double> Intersects = quantity.Intersect( quantity1 );
            foreach ( double item in Intersects )
            {
                Console.Write( item+" " );
            }
            Console.WriteLine( );
            Console.WriteLine( "Except【出现多少个不重复】:" );
            IEnumerable<double> Excepts = quantity.Except( quantity1 );
            foreach ( double item in Excepts )
            {
                Console.Write( item + " " );
            }
            Console.WriteLine( );
            Console.WriteLine( "Range方法的使用:3,表示从数字3开始,10,表示10个数字" );
            var coolmath1 = Enumerable.Range( 3, 10 ).Reverse( );
            foreach ( int item in coolmath1 )
            {
                Console.Write( item+" " );
            }
            Console.WriteLine( );
            Console.WriteLine( "Select( x => x * 5 ) x表示集合中的每一个数字:" );
            var coolmath = Enumerable.Range( 3, 10 ).Select( x => x * 5 );
            foreach ( int item in coolmath )
            {
                Console.Write( item+" " );
            }
        }
        private static void stringQueryFunction( )
        {
            string[ ] name = { "赵丽蓉", "马季", "侯耀文", "马三立", "侯宝林", "乔布斯" };
            //模糊查询
            //IEnumerable<string> val = from num in name where num.StartsWith( "马" ) select num;
            //排序
            IEnumerable<string> val = from num in name orderby num select num;
            var mum = (from m in name select m).First();
            Console.WriteLine( "var First()返回指定的条件:" );
            foreach ( var item in mum )
            {
                Console.WriteLine( item);
            }
            Console.WriteLine( "+++++++++++++++++");
            foreach ( var item in val )
            {
                Console.WriteLine( item );
            }
        }
        private static void IntQueryFunction( )
        {
            int[ ] age = new int[ ] { 5, 6, 78, 45, 4, 768, 899, 67, 5, 900 };
            var numQuery = from num in age where num > 800 && num <= 900 select num;
            IEnumerable<int> query = ( from c in age orderby c select c ).Take( 4 );
            Console.WriteLine( "Take( 4 )指定出现4个" );
            foreach ( var item in query )
            {
                Console.WriteLine( item);
            }
            Console.WriteLine("=============================");
            Console.WriteLine( );
            Console.WriteLine( "age where num > 800 && num <= 900的有: " );
            foreach ( var item in numQuery )
            {
                Console.Write( "{0} ", item );
            }
            Console.WriteLine( );
        }
    }
}

转载于:https://www.cnblogs.com/yuan-2012/archive/2012/08/19/2646034.html

你可能感兴趣的文章
SSAS:菜鸟笔记(二)定义计算(DMX脚本)
查看>>
mysql max_allowed_packet查询和修改
查看>>
[NOI1999] 棋盘分割
查看>>
10.25作业
查看>>
Sphinx配置文件分析
查看>>
INFINITY的一个坑
查看>>
fabric文件上传打包与校验
查看>>
Nginx数据结构之红黑树ngx_rbtree_t
查看>>
WWDC 2010学习笔记和目录
查看>>
IDEA 安装scala插件
查看>>
Redis集群配置
查看>>
教你如何在Protel中显示标题栏内容
查看>>
设计模式 策略模式 以角色游戏为背景
查看>>
面试题目——《CC150》高等难题
查看>>
Java-io流入门到精通详细总结
查看>>
数字逗号标记—以前原创(一)
查看>>
Struts1基于Json数据格式的ExtJs单选树(Struts1+Spring+Hibernate)
查看>>
[HNOI2019]白兔之舞(MTT+单位根反演)
查看>>
面向对象基础-集合
查看>>
Eclipse使用EclEmma看单元测试的代码覆盖率
查看>>