Пример #1
0
        public static Student Add(Student stu)
        {
            using (var context = new StudentContext())
            {
#if DEBUG
                // Log query generated to output window
                context.Database.Log = Console.WriteLine;
#endif
                // Generating/preparing insert query
                context.Students.Add(stu);
                // Executing insert query against DB
                context.SaveChanges();

                // Return the student with the StudentId set
                return(stu);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns a list of all the students
        /// sorted by StudendId in ascending order
        /// </summary>
        /// <returns></returns>
        public static List <Student> GetAllStudents()
        {
            //var context = new StudentContext(); //optional when type is explicit
            StudentContext context = new StudentContext();

            // LINQ - Language INtegrated Query
            // LINQ Query Syntax
            List <Student> students =
                (from s in context.Students
                 orderby s.StudentId ascending
                 select s).ToList();

            // LINQ Method Syntax
            List <Student> students2 = context.Students
                                       .OrderBy(stu => stu.StudentId)
                                       .ToList();

            return(students);
        }