static void Run1()
        {
            var _context = new eddbContext();
            //_context.Students.ToList()
            //    .ForEach(s => Console.WriteLine($"{s.Firstname} {s.Lastname}"));

            //var majors = from m in _context.Majors
            //             where m.MinSat > 1000
            //             orderby m.Description
            //             select m;

            //foreach(var m in majors) {
            //    Console.WriteLine($"{m.Description} | {m.MinSat}");
            //}

            var studentmajor = (from s in _context.Students
                                join m in _context.Majors
                                on s.MajorId equals m.Id
                                select new {
                Name = s.Firstname + " " + s.Lastname,
                Major = m.Description
            }).ToList();

            studentmajor.ForEach(s => Console.WriteLine($"{s.Name} | {s.Major}"));
        }
        static void Run1()
        {
            // this is Method Syntax
            var _context = new eddbContext();

            _context.Students.ToList()
            .ForEach(s => Console.WriteLine($"{s.Firstname} {s.Lastname}"));

            // this is Qurey Syntax
            var majors = from m in _context.Majors
                         where m.MinSat > 1000
                         orderby m.Description
                         select m;

            foreach (var m in majors)
            {
                Console.WriteLine($"{m.Description} | {m.MinSat}");
            }
            // join Students & Majors. print Name and Major
            var allStudents = (from s in _context.Students
                               join m in _context.Majors.DefaultIfEmpty()
                               on s.MajorId equals m.Id into grp
                               from mm in grp.DefaultIfEmpty()
                               select new {
                Name = s.Firstname + " " + s.Lastname,
                Major = mm == null ? "undeclared" : mm.Description
            }).ToList();                    // a lot of stuff happening here, have becca or her mom better explain

            allStudents.ForEach(s => Console.WriteLine($"{s.Name} - {s.Major}"));
        }
Beispiel #3
0
        static void Run1()
        {
            var _context = new eddbContext();           // setting an instance of the database


            var students = _context.Students.ToList();  // setting the student variable

            foreach (var s in students)
            {
                Console.WriteLine($"{s.Firstname} {s.Lastname} ");
            }

            Console.WriteLine($" ");

            var _context2 = new eddbContext();                  // easier way to do above

            foreach (var s in _context2.Students.ToList())      // plugging the context straight into the collection
            {
                Console.WriteLine($"{s.Firstname} {s.Lastname} ");
            }

            Console.WriteLine($" ");

            var _context3 = new eddbContext();                  // easiest way to do above

            _context3.Students.ToList()
            .ForEach(s => Console.WriteLine($"{s.Firstname} {s.Lastname} "));

            Console.WriteLine($"");

            var majors = from m in _context.Majors      // show all the majors that require a minSAT over 1000
                         where m.MinSat > 1000
                         orderby m.Description
                         select m;

            foreach (var m in majors)
            {
                Console.WriteLine($"{m.Description} || {m.MinSat}");
            }

            Console.WriteLine($"");

            // show all student with their majors
            var allstudents = (from s in _context.Students      // inner join example
                               join m in _context.Majors        //.DefaultIfEmpty()
                               on s.MajorId equals m.Id
                               orderby s.Lastname
                               select new
            {
                Name = s.Firstname + " " + s.Lastname,
                Major = s.MajorId == null ? "Undeclared" : m.Description
            }).ToList();

            foreach (var s in allstudents)
            {
                Console.WriteLine($"{s.Name} | {s.Major}");
            }

            Console.WriteLine($" ");


            var allstudents2 = (from s in _context.Students       // outter join example of above
                                join m in _context.Majors         // getting all students including ones without a major
                                on s.MajorId equals m.Id into grp // grp is a short term holding collection to run the code
                                from mm in grp.DefaultIfEmpty()   // "mm" is the major
                                select new
            {
                Name = s.Firstname + " " + s.Lastname,
                Major = s.MajorId == null ? "Undeclared" : mm.Description                       // if there is a major that is null it will change it to "Undeclared"
            }).ToList();

            foreach (var s in allstudents2)
            {
                Console.WriteLine($"{s.Name} | {s.Major}");
            }
        }
 public MajorController()     // default constructor
 {
     _context = new eddbContext();
 }
 public StudentsController()
 {
     _context = new eddbContext(); // this is to be able to control the data we put in and write it.
 }
 public MajorController()
 {
     _context = new eddbContext();
 }
 public StudentsController()
 {
     _context = new eddbContext();
 }
 public StudentsController()     // default constructor
 {
     _context = new eddbContext();
 }