Пример #1
0
        public static void QueryCarsDb()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            var query = from car in db.Cars
                        orderby car.Combined descending, car.Name ascending
            select car;

            var query2 = db.Cars.OrderByDescending(c => c.Combined).ThenBy(c => c.Name).Take(10);

            foreach (var car in query.Take(10))
            {
                Console.WriteLine($"{car.Name}:\t{car.Combined}");
            }
        }
Пример #2
0
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            //var query = from car in db.Cars
            //            orderby car.Combined descending, car.Name ascending
            //            select car;
            var query =
                db.Cars.OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name);

            foreach (var car in query.Take(10))
            {
                Console.WriteLine($"{car.Name}: {car.Combined}");
            }
        }
Пример #3
0
        private static void InsertData()
        {
            var carsi = ProcessCars("fuel.csv");

            var db = new CarDb();

            //db.Database.Log = Console.WriteLine;//ispise ti log vezan za db u cmd


            if (!db.Cars.Any())
            {
                foreach (var car in carsi)
                {
                    db.Cars.Add(car);
                }
                db.SaveChanges();
            }
        }
Пример #4
0
        private static void InsertData()
        {
            var cars = ProcessCars("fuel.csv");

            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            if (!db.Cars.Any())
            {
                foreach (var car in cars)
                {
                    db.Cars.Add(car);
                }

                db.SaveChanges();
            }
        }
Пример #5
0
        private static void InsertData()
        {
            var cars = ProcessCars("fuel.csv");

            var db = new CarDb();

            //this will log the SQL query that has been sent to the db
            db.Database.Log = Console.WriteLine;

            if (!db.Cars.Any())
            {
                foreach (var car in cars)
                {
                    db.Cars.Add(car);
                }

                db.SaveChanges();
            }
        }
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;
            var query = from car in db.Cars
                        orderby car.Combined descending, car.Name ascending
            select car;

            var query2 = db.Cars.Where(c => c.Manufacturer == "BMW")
                         .OrderByDescending(c => c.Combined)
                         .ThenBy(c => c.Name)
                         .Take(10)
                         .ToList();

            var query3 =
                db.Cars.GroupBy(c => c.Manufacturer)
                .Select(g => new
            {
                Name = g.Key,
                Cars = g.OrderByDescending(c => c.Combined).Take(2)
            });
            var query4 =
                from car in db.Cars
                group car by car.Manufacturer into manufacturer
                select new
            {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer
                        orderby car.Combined descending
                        select car).Take(2)
            };

            foreach (var group in query4)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }
        }
Пример #7
0
        private static void QueryData()
        {
            var db = new CarDb();

            var query = from car in db.Cars
                        orderby car.Combined descending, car.Name ascending
            select car;

            var queryExt =
                db.Cars.OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name)
                .Take(10);

            foreach (var car in queryExt)
            {
                Console.WriteLine($"{car.Name}: {car.Combined}");
            }

            Console.ReadLine();
        }
Пример #8
0
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            var query = from car in db.Cars
                        orderby car.Combined descending, car.Name ascending
            select car;

            var query2 =
                db.Cars.Where(c => c.Manufacturer == "BMW")
                .OrderByDescending(c => c.Combined)
                .ThenBy(c => c.Name)
                .Take(10)
                .ToList();

            foreach (var car in query2)
            {
                Console.WriteLine($"{car.Name}: {car.Combined}");
            }
        }
Пример #9
0
        //Entity and Linq
        private static void QueryData()
        {
            var db = new CarDb();

            //db.Database.Log = Console.WriteLine;  not a method on dotnet core

            var query = from car in db.Cars
                        orderby car.Combined descending, car.Name ascending
            select car;


            foreach (var car in query.Take(10))
            {
                Console.WriteLine($"{ car.Name}: {car.Combined}");
            }

            var extentionQuery =
                db.Cars.OrderByDescending(c => c.Combined).ThenBy(c => c.Name).Take(10);

            foreach (var car in extentionQuery)
            {
                Console.WriteLine($"{ car.Name}: {car.Combined}");
            }
        }
        //// METHOD SYNTAX
        //private static void QueryData() {
        //    var db = new CarDb();

        //    db.Database.Log = Console.WriteLine;

        //    var query =
        //        db.Cars.GroupBy(c => c.Manufacturer)
        //                .Select(g => new {
        //                    Name = g.Key,
        //                    Cars = g.OrderByDescending(c => c.Combined).Take(2)
        //                });

        //    foreach (var group in query) {
        //        Console.WriteLine(group.Name);
        //        foreach (var car in group.Cars) {
        //            Console.WriteLine($"\t{car.Name}: {car.Combined}");
        //        }
        //    }

        //    //Console.WriteLine(query.Count());
        //    //foreach (var car in query) {
        //    //    Console.WriteLine($"{car.Name}: {car.Combined}");
        //    //}
        //}

        // QUERY SYNTAX
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            var query = from car in db.Cars
                        group car by car.Manufacturer into manufacturer
                        select new {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer
                        orderby car.Combined descending
                        select car).Take(2)
            };

            foreach (var group in query)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name}: {car.Combined}");
                }
            }
        }
Пример #11
0
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine; //logs the database log to the console -- a way to check that the information is going to the database without checking the database itself

            //----query syntax
            var query =
                from car in db.Cars
                orderby car.Combined descending, car.Name ascending
            select car;

            //---extension method syntax

            //Note that EF will only work with IQueryable types. If the method calls for IEnumerables, it must be done in memory.

            /* var queryEx =
             *   db.Cars.Where(c => c.Manufacturer == "BMW")
             *          .OrderByDescending(c => c.Combined)
             *          .ThenBy(c => c.Name)
             *          // .Take(10)
             *          // .Select(c => new { Name = c.Name.Split(" ") })
             *          // .ToList();
             *          // note how this doesn't work because EF can not understand Split and this works as an IQueryable
             *          .Take(10)
             *          .ToList()
             *          .Select(c => new { Name = c.Name.Split(' ') }); //here we are working in memory and can use methods such as split
             *
             * foreach (var car in queryEx)
             * {
             *   for (var i = 0; i < car.Name.Length; i++)
             *   {
             *       Console.WriteLine($"{car.Name[i]}");
             *   }
             * } */

            //-- more complex syntax for EF using Ex. Methods

            var query2 =
                db.Cars.GroupBy(c => c.Manufacturer)
                .Select(g => new
            {
                Name = g.Key,
                Cars = g.OrderByDescending(c => c.Combined).Take(2)
            });


            //-- executing same code using query syntax

            var query3 =
                from car in db.Cars
                group car by car.Manufacturer into manufacturer
                select new
            {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer     //can even use query syntax within anon constructor.
                        orderby car.Combined descending
                        select car).Take(2)
            };

            foreach (var group in query3)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }
        }
Пример #12
0
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;

            #region Complex

            // query syntex
            var query2 =
                from car in db.Cars
                group car by car.Manufacturer
                into manufacturer
                select new
            {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer
                        orderby car.Combined descending
                        select car).Take(2)
            };

            // Extention method syntex
            var query = db.Cars.GroupBy(c => c.Manufacturer)
                        .Select(g => new
            {
                Name = g.Key,
                Cars = g.OrderByDescending(c => c.Combined).Take(2)
            });

            foreach (var group in query2)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t {car.Name} : {car.Combined}");
                }
            }

            #endregion

            #region Simple

            //var query = from car in db.Cars
            //    orderby car.Combined descending, car.Name
            //    select (car);

            //foreach (var car in query.Take(10))
            //{
            //    Console.WriteLine($"{car.Name} : {car.Combined}");
            //}
            //var query = db.Cars.Where(c => c.Manufacturer == "BMW")
            //    .OrderByDescending(c => c.Combined)
            //    .ThenBy(c => c.Name)
            //    .Take(10) // without tolist it run on the sql  IEnumeramble
            //    .Select(c=> new {Name = c.Name.ToUpper()})
            //    .ToList(); // with tolist rum in the memory  IQueryable

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

            //foreach (var car in query)
            //{
            //    Console.WriteLine($"{car.Name} : {car.Combined}");
            //}


            #endregion
        }
Пример #13
0
        private static void QueryData()
        {
            var db = new CarDb();

            db.Database.Log = Console.WriteLine;


            var query =
                from car in db.Cars
                group car by car.Manufacturer into manufacturer
                select new {
                Name = manufacturer.Key,
                Cars = (from car in manufacturer
                        orderby car.Combined descending
                        select car)
                       .Take(2)
            };



            //var query =
            //    from car in db.Cars
            //    group car by car.Manufacturer into manufacturer
            //    select new {
            //        Name = manufacturer.Key,
            //        Cars = manufacturer.OrderByDescending(c => c.Combined).Take(2)
            //    };



            //var query = db.Cars
            //    .GroupBy(c => c.Manufacturer)
            //    .Select(g => new {
            //        Name = g.Key,
            //        Cars = g.OrderByDescending(c => c.Combined).Take(2)
            //    });

            foreach (var group in query)
            {
                Console.WriteLine(group.Name);
                foreach (var car in group.Cars)
                {
                    Console.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }



            //var query =
            //    from car in db.Cars
            //    orderby car.Combined descending, car.Name ascending
            //    select car;

            //var query = db.Cars
            //    .Where(c => c.Manufacturer == "BMW")
            //    .OrderByDescending(c => c.Combined)
            //    .ThenBy(c => c.Name)
            //    .Take(10)
            //    .ToList();
            //.Select(c => new { Name = c.Name.Split(' ') }); // not supported by entity framework


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

            //Console.WriteLine(query.Count());

            //foreach (var car in query) {
            //    Console.WriteLine($"{car.Name} : {car.Combined}");
            //}
        }