示例#1
0
文件: Program.cs 项目: jdoble97/LINQ
        public static void ObtenerEmpresas()
        {
            IEnumerable <Empleado> query = from empleado in empleados
                                           join empresa in empresas on empleado.EmpresaId equals empresa.Id
                                           where empresa.Nombre == "Facebook"
                                           select empleado;

            Console.WriteLine("Los siguientes empleados trabajan en Facebook");
            LinqExample <Empleado> .PrintDatosObjetos(query);
        }
示例#2
0
文件: Program.cs 项目: jdoble97/LINQ
        static void Main(string[] args)
        {
            int[] source = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            LinqExample <int> .FindEvenNumbers(source);

            empresas = new List <Empresa>
            {
                new Empresa {
                    Id = 1, Nombre = "Google"
                },
                new Empresa {
                    Id = 2, Nombre = "Facebook"
                }
            };
            empleados = new List <Empleado>
            {
                new Empleado {
                    Id = 1, Cargo = "Programador", EmpresaId = 1, Nombre = "Jorge", Salario = 1200
                },
                new Empleado {
                    Id = 2, Cargo = "CEO", EmpresaId = 1, Nombre = "Joel", Salario = 1200
                },
                new Empleado {
                    Id = 3, Cargo = "Analista", EmpresaId = 2, Nombre = "Kate", Salario = 1200
                },
                new Empleado {
                    Id = 4, Cargo = "Administrador", EmpresaId = 2, Nombre = "Luis", Salario = 1200
                },
                new Empleado {
                    Id = 5, Cargo = "Programador Senior", EmpresaId = 1, Nombre = "Emely", Salario = 1200
                }
            };
            //TestEmpresa();
            //EmpleadosOrdenados();
            ObtenerEmpresas();
        }
示例#3
0
        private static void LinqExample()
        {
            LinqExample myLinq = new LinqExample();

            myLinq.ShowmeLinq();
        }
示例#4
0
        private static void MyLinqSample()
        {
            LinqExample myLinq = new LinqExample();

            myLinq.ShowMeLinq();
        }
示例#5
0
文件: Program.cs 项目: jdoble97/LINQ
        public static void EmpleadosOrdenados()
        {
            IEnumerable <Empleado> empleadosOrdenados = from empleado in empleados orderby empleado.Nombre descending select empleado;

            LinqExample <Empleado> .PrintDatosObjetos(empleadosOrdenados);
        }
示例#6
0
文件: Program.cs 项目: jdoble97/LINQ
        public static void TestEmpresa()
        {
            IEnumerable <Empleado> query = from empleado in empleados where empleado.Cargo == "CEO" select empleado;

            LinqExample <Empleado> .PrintDatosObjetos(query);
        }
示例#7
0
        // This is basically a runner class, runs various classes implemented.
        public static void Main(string[] args)
        {
            //var datalist = new StoreData<int>();
            //datalist.Add(1);
            //datalist.Add(2);
            //datalist.Add(3);
            //datalist.Add(4);
            //datalist.Add(5);

            //foreach (var item in datalist)
            //{
            //    Console.WriteLine(item);
            //}

            //var datalist2 = new StoreData<string>();
            //datalist2.Add("If ");
            //datalist2.Add("Only ");
            //datalist2.Add("I ");
            //datalist2.Add("Could ");
            //datalist2.Add("***.");

            //foreach (var item in datalist2)
            //{
            //    Console.Write(item);
            //}
            //Console.ReadLine();


            A dataA = new A()
            {
                Data1 = "Hello", Data2 = "World"
            };
            // Assigning like this does a "shallow copy".
            A dataB = dataA;

            dataB.Data1 = "Goodbye";
            dataB.Data2 = "City";

            A dataBDistinct = new A(dataA);

            // dataBDistinct should not be affected by changes made to dataA
            dataA.Data1 = "Hello";
            dataA.Data2 = "World";

            StructExample someStruct      = new StructExample();
            StructExample someOtherStruct = new StructExample(1, 2);

            //FileInfo fi = new FileInfo("ads");
            //fi.Attributes = FileAttributes.ReadOnly | FileAttributes.ReadOnly;

            Type predefineAttribute = typeof(AttributeUsageAttribute);
            Type conditionalAttr    = typeof(ConditionalAttribute);

            foreach (Attribute attr in  predefineAttribute.GetCustomAttributes(false))
            {
                Console.WriteLine(attr.ToString());
            }
            foreach (PropertyInfo poperty in predefineAttribute.GetProperties())
            {
                Console.WriteLine(poperty.ToString());
            }

            SomeObsoleteMethod();

            IMyInterfaceCompression compression = new ZipCompression();

            compression.Compress("", null);
            compression = new RarCompression();
            compression.Compress("", null);

            Console.WriteLine("***************Async****************");
            TestAsync();
            // Console.WriteLine("***************Async****************");

            Console.WriteLine("***************LINQ EXAMPLE ****************");
            LinqExample db        = new LinqExample();
            var         customers = from cust in db.Customers
                                    select cust;

            db.AddCustomer(new Customer()
            {
                Name = "NitinAr", Designation = "Technical Fellow", City = "Seattle"
            });
            foreach (Customer cst in customers)
            {
                Console.Write(cst.Name + ", ");
            }
            Console.WriteLine();
            Console.WriteLine("***************LINQ EXAMPLE ****************");

            Console.WriteLine("*************** DELEGATES, LAMBDAS ****************");
            Func <int, int, bool> comparisonHandler = LessThan;

            comparisonHandler = (a, b) => a < b;
            comparisonHandler = delegate(int a, int b)
            {
                return(a < b);
            };
            Console.WriteLine("***************DELEGATES, LAMBDAS****************");

            Console.WriteLine("***************vs EXPRESSIONS****************");
            // Expression tree is a collection of Data and by iterating over data, it is possible to convert
            // data to another type.
            // Look at the different in calling ToString() on Func<> v/s Expression<>.
            Expression <Func <int, int, bool> > comparisonHandlerExpression;

            comparisonHandlerExpression = (x, y) => x < y;

            Console.WriteLine(comparisonHandler.ToString());
            Console.WriteLine(comparisonHandlerExpression.ToString());
            Console.WriteLine(comparisonHandlerExpression.Body.ToString());
            Console.WriteLine("***************vs EXPRESSIONS****************");

            CloseOverMe     closeOverMe = new CloseOverMe();
            Func <int, int> incrementer = closeOverMe.GetFuncIncrementer();

            Console.WriteLine(incrementer(3));
            Console.WriteLine(incrementer(4));

            Console.ReadLine();
        }
示例#8
0
        static void Main(string[] args)
        {
            // Make sure the caller supplied a host name.
            if (args.Length == 0 || args[0].Length == 0)
            {
                // Print a message and exit.
                Console.WriteLine("You must specify the name of a host computer.");
                return;
            }
            // Start the asynchronous request for DNS information.
            IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);

            Console.WriteLine("Processing request for information...");
            // Wait until the operation completes.
            result.AsyncWaitHandle.WaitOne();
            // The operation completed. Process the results.
            try
            {
                // Get the results.
                IPHostEntry host      = Dns.EndGetHostEntry(result);
                string[]    aliases   = host.Aliases;
                IPAddress[] addresses = host.AddressList;
                if (aliases.Length > 0)
                {
                    Console.WriteLine("Aliases");
                    for (int i = 0; i < aliases.Length; i++)
                    {
                        Console.WriteLine("{0}", aliases[i]);
                    }
                }
                if (addresses.Length > 0)
                {
                    Console.WriteLine("Addresses");
                    for (int i = 0; i < addresses.Length; i++)
                    {
                        Console.WriteLine("{0}", addresses[i].ToString());
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Exception occurred while processing the request: {0}",
                                  e.Message);
            }


            Console.ReadLine();



            // Linq
            LinqExample ex = new LinqExample();
            //ex.LinqConsole();



            student_a entity1 = new student_a();

            #region 字段属性,抽象方法,虚方法

            //entity1.Test(); // 普通方法
            //entity1.Abstract_Test(); // 抽象方法
            //entity1.Abstract_Test1(); // 抽象方法

            //entity1.Virtual_Test();// 虚方法
            //entity1.Virtual_Test1();// 重写虚方法

            // entity1.Name = "北京欢迎你";
            // Console.WriteLine(entity1.Name);

            //entity1.setAge(11);
            //Console.WriteLine(entity1.getAge().ToString());

            #endregion

            #region  委托

            //delegate_test dt = new delegate_test();
            //dt.main();

            #endregion

            #region 泛型

            #region ArrayList
            //System.Collections.ArrayList list = new System.Collections.ArrayList();
            //// Add an integer to the list.
            //list.Add(3);
            //// Add a string to the list. This will compile, but may cause an error later.
            //list.Add("It is raining in Redmond.");

            //int t = 0;
            //// This causes an InvalidCastException to be returned.
            //foreach (int x in list)
            //{
            //    t += x;
            //}

            //List<int> listtt = new List<int> {1,2,3,4,5 };

            #endregion

            #region 参数委托约束
            if (1 > 3)
            {
                var map = Test2.EnumNameValues <RainBow>();

                foreach (var pair in map)
                {
                    Console.WriteLine($"{pair.Key}:\t{pair.Value}");
                }
            }
            #endregion

            #region 泛型接口

            if (1 > 3)
            {
                //Person is the type argument.
                SortedList <Person> list = new SortedList <Person>();

                //Create name and age values to initialize Person objects.
                string[] names = new string[] { "a", "b", "c" };

                int[] ages = new int[] { 1, 2, 3 };

                //Populate the list.
                for (int x = 0; x < 3; x++)
                {
                    list.AddHead(new Person(names[x], ages[x]));
                }

                //Print out unsorted list.
                foreach (Person p in list)
                {
                    System.Console.WriteLine(p.ToString());
                }
                System.Console.WriteLine("Done with unsorted list");

                //Sort the list.
                list.BubbleSort();

                //Print out sorted list.
                foreach (Person p in list)
                {
                    System.Console.WriteLine(p.ToString());
                }
                System.Console.WriteLine("Done with sorted list");
            }
            #endregion

            #endregion

            #region 元组
            if (1 > 3)
            {
                var ToString    = "This is some text";
                var one         = 1;
                var Item1       = 5;
                var projections = (ToString, one, Item1);
                // Accessing the first field:
                Console.WriteLine(projections.Item1);
                //Console.WriteLine(projections.ToStrings);
                // There is no semantic name 'ToString'
                // Accessing the second field:
                Console.WriteLine(projections.one);
                Console.WriteLine(projections.Item2);
                // Accessing the third field:
                Console.WriteLine(projections.Item3);
                // There is no semantic name 'Item`.

                var pt1 = (X : 3, Y : 0);
                var pt2 = (X : 3, Y : 4);

                var xCoords = (pt1.X, pt2.X);
                // There are no semantic names for the fields
                // of xCoords.

                // Accessing the first field:
                Console.WriteLine(xCoords.Item1);
                // Accessing the second field:
                Console.WriteLine(xCoords.Item2);
            }
            if (1 > 3)
            {
                // The 'arity' and 'shape' of all these tuples are compatible.
                // The only difference is the field names being used.
                var unnamed        = (42, "The meaning of life");
                var anonymous      = (16, "a perfect square");
                var named          = (Answer : 42, Message : "The meaning of life");
                var differentNamed = (SecretConstant : 42, Label : "The meaning of life 123456");


                unnamed = named;
                //  Console.WriteLine($"{unnamed.Item1}, {unnamed.Message}");

                named = unnamed;
                // 'named' still has fields that can be referred to
                // as 'answer', and 'message':
                Console.WriteLine($"{named.Answer}, {named.Message}");

                // unnamed to unnamed:
                anonymous = unnamed;

                // named tuples.
                named = differentNamed;
                // The field names are not assigned. 'named' still has
                // fields that can be referred to as 'answer' and 'message':
                Console.WriteLine($"{named.Answer}, {named.Message}");
                (long, string)conversion = named;

                Console.WriteLine($"{conversion.Item1}, {conversion.Item2}");
            }

            #endregion

            #region 运算符

            if (1 > 3)
            {
                string[] arrNum = { "zero", "one", "two", "three", "four", "five", "six", "seven" };

                var shortDig = arrNum.Where((a, index) => a.Length < index);

                foreach (var sd in shortDig)
                {
                    Console.WriteLine(sd);
                }
            }


            #endregion

            #region 迭代器
            if (1 > 3)
            {
                DaysOfTheWeek week = new DaysOfTheWeek();
                foreach (string day in week)
                {
                    Console.WriteLine(day + " _ ");
                }
            }

            #endregion



            Console.ReadLine();
        }
示例#9
0
        static void Main(string[] args)
        {
            LinqExample.LambdaExpressionGenericUsefulMethods();

            Console.ReadLine();
        }