Exemple #1
0
        static void Main(string[] args)
        {
            var lst = new PersonNuGetPackage.PersonRepositoryRandom().Get10People();

            foreach (var p in lst)
            {
                Console.WriteLine(p.Name);
            }

            Console.WriteLine();
            var res1  = from p in lst select p;
            var res11 = lst;

            foreach (var p in res11)
            {
                Console.WriteLine(p.Name);
            }

            Console.WriteLine();
            var res2 = from p in lst orderby p.Gender, p.Name select p;
            var res22 = lst.OrderBy(i => i.Gender).ThenBy(i => i.Name);

            foreach (var p in res22)
            {
                Console.WriteLine(p.Name + " " + p.Gender);
            }

            Console.WriteLine();
            var res3 = from p in lst where p.Height > 150 && p.IsHealthy == true orderby p.Gender, p.Name select p;
            var res33 = lst.Where(i => i.Height > 150 && i.IsHealthy)
                        .OrderBy(i => i.Gender)
                        .ThenBy(i => i.Name);

            foreach (var p in res33)
            {
                Console.WriteLine(p.Name + " " + p.Gender);
            }


            Console.WriteLine();
            //var res4 = from p in lst where p.Height > 150 && p.IsHealthy == true orderby p.Gender, p.Name select p;
            var res44 = lst.Where(i => i.Height > 150 && i.IsHealthy)
                        .OrderBy(i => i.Gender)
                        .ThenBy(i => i.Name)
                        .Select(i => new { Navn = i.Name, Højde = i.Height });

            foreach (var p in res44)
            {
                Console.WriteLine(p.Navn);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // EF (...LINQ2Sql)

            List <int> tal = new List <int>()
            {
                1, 7, 8, 17, 6, 4, 6
            };

            var res1 = (from x in tal
                        where x < 10
                        orderby x
                        select x).ToList();

            var res2 = tal.Where(i => i < 10).OrderBy(i => i).ToList();

            var res3 = tal.OrderBy(i => i).GroupBy(i => i);

            var lst = new PersonNuGetPackage.PersonRepositoryRandom().GetPeople(100);
            //foreach (var item in lst)
            //{
            //    Console.WriteLine(item.Name);
            //}

            var res4 = lst.Where(i => i.Height < 170 && i.Gender == PersonNuGetPackage.Gender.Female).OrderBy(i => i.Name).ThenBy(i => i.Height);

            var res5 = lst
                       .OrderBy(i => i.Name)
                       .Where(i => i.Height < 180)
                       .GroupBy(i => i.Gender);
        }
        static void Main(string[] args)
        {
            List <int> lst1 = new List <int>();

            lst1.Add(1);
            lst1.Add(5);
            lst1.Add(6);
            lst1.Add(66);
            lst1.Add(33);
            lst1.Add(789);
            lst1.Add(2);
            lst1.Add(7);

            var r1 = from i in lst1 where i > 4 select i;

            //List<int> r1 = (from i in lst1 where i > 4 select i).ToList();

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

            // brug tolist eller todictionary for at køre udtryk

            var p  = new PersonNuGetPackage.PersonRepositoryRandom().Get10People();
            var r2 = from person in p
                     where person.Height > 170 && person.IsHealthy
                     select person.Name;

            var r4 = from person in p
                     where person.Height > 170 && person.IsHealthy
                     orderby person.Height, person.Name
                select new { Navn = person.Name, Højde = person.Height };

            // sql syntaks
            var r5 = from person in p
                     where person.Height > 170 && person.IsHealthy
                     select person.Name;
            // lambda syntaks
            var r6 = p.Where(i => i.Height > 170 && i.IsHealthy == true).
                     OrderBy(i => i.Height).
                     ThenBy(i => i.Name).
                     Select(i => new { Navn = i.Name, Højde = i.Height });



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


            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Console.Write("Press any key to continue . . . ");
                System.Console.ReadKey();
            }
        }