private static void DynamicLinq(EntitiesModel context) { // strongly typed dynamic LINQ Console.WriteLine("Dynamic strongly typed LINQ Where:"); var query1 = context.Cars.Where("CarYear < @0", 2000).ToList(); Console.WriteLine("Car\tCategory"); foreach (var car in query1) { Console.WriteLine("{0}\t{1}", car.CarID, car.Category.CategoryName); } // weakly typed dynamic LINQ with projection Console.WriteLine("Dynamic weakly typed LINQ Where and Select:"); var objList = context.GetAll("OpenAccessModel9.Car").Where("CarYear < @0", 2000) .Select("new (CarID as Id, CarYear)") .Cast<object>().ToList(); foreach (object objCar in objList) { Console.WriteLine(objCar); } }
private static void BulkDelete(EntitiesModel context) { // avoid showing the INSERT statements that populate the data context.Log = null; for (int i = 0; i < 10; i++) { Car car = new Car() { Make = "BMW", Model = "1020", CarYear = 2018, Available = true, CategoryID = 1, TagNumber = i.ToString()}; context.Add(car); } context.SaveChanges(); // show the delete statements context.Log = Console.Out; var query = context.GetAll<Car>().Where(c => c.CarYear > DateTime.Now.Year); int deleted = query.DeleteAll(); Console.WriteLine("Deleted cars: {0}", deleted); }
private static void BulkUpdate(EntitiesModel context) { string tooOldName = "Too old"; Category tooOldCategory = context.Categories.FirstOrDefault(c => c.CategoryName == tooOldName); if (tooOldCategory == null) { tooOldCategory = new Category(); tooOldCategory.CategoryName = tooOldName; context.Add(tooOldCategory); context.SaveChanges(); } var query = context.GetAll<Car>().Where(c => c.CarYear < 2000); int updated = query.UpdateAll(u => u.Set(c => c.Category, c => tooOldCategory) .Set(c => c.Available, c => false)); Console.WriteLine("Updated cars: {0}", updated); }