Ejemplo n.º 1
0
        private static void LinqSelect()
        {
            List <int> listint = new List <int> {
                1, 2, 3
            };

            Console.WriteLine(string.Join(",", listint));

            var listint2 = (from c in listint
                            select c + 2).ToList();

            Console.WriteLine(string.Join(",", listint2));

            List <LinqClass> linqClasses = new List <LinqClass> {
            };
            LinqClass linqClass1         = new LinqClass
            {
                Index = 1,
                Name  = "第一个值"
            };

            LinqClass linqClass2 = new LinqClass
            {
                Index = 2,
                Name  = "第二个值"
            };

            linqClasses.Add(linqClass1);
            linqClasses.Add(linqClass2);

            foreach (var item in linqClasses)
            {
                Console.WriteLine(item.Index + "  " + item.Name);
            }

            var dealer = (from c in linqClasses
                          where c.Index > 0
                          select c
                          ).FirstOrDefault();
            var testCar = (from c in linqClasses
                           orderby c.Index descending
                           select c
                           ).FirstOrDefault();

            var testCar2 = (from c in linqClasses
                            orderby c.Index ascending
                            select c
                            ).FirstOrDefault();
        }
Ejemplo n.º 2
0
        static void DisplayLinqExpressions()
        {
            int test = -1;

            while (test != 0)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.Write("Linq - Enter exercise no: (1 to 4) ");
                string input = Console.ReadLine();

                Console.Clear();
                int.TryParse(input, out test);
                Console.ForegroundColor = ConsoleColor.Green;
                switch (test)
                {
                case 0:
                    break;

                case 1:
                    LinqClass Lq = new LinqClass("Linq expressions - ");
                    break;

                case 2:
                    Console.WriteLine("Not implemented!");
                    break;

                case 3:
                    Console.WriteLine("Not implemented!");
                    break;

                case 4:
                    Console.WriteLine("Not implemented!");
                    break;

                default:
                    Console.WriteLine("Not implemented!");
                    break;
                }
            }
        }