Example #1
0
        static void CopyToDatabase(MyownContext context)
        {
            Repository repo = new Repository();

            context.Groups.AddRange(repo.Groups);
            context.Students.AddRange(repo.Students);

            context.SaveChanges();
        }
Example #2
0
        static void FilterAndOrder(MyownContext context)
        {
            var result = from s in context.Students
                         where s.BirthDate.Year >= 1999
                         orderby s.Rating descending, s.Name
            select s;

            foreach (var s in result)
            {
                Console.WriteLine("{0} {1}: {2}", s.Name, s.Surname, s.Rating);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            using (MyownContext c = new MyownContext())
            {
                // Uncomment and run once to fill the database
                // CopyToDatabase(c);

                // Queries
                //FilterAndOrder(c);
                //GroupByFirstLetter(c);
                Join(c);
            }
            Console.ReadKey();
        }
Example #4
0
        static void GroupByFirstLetter(MyownContext context)
        {
            var result = from s in context.Students
                         group s by s.Name.Substring(0, 1) into g
                         select g;

            foreach (var group in result)
            {
                Console.WriteLine("\nFirst letter: {0}", group.Key);
                foreach (var s in group)
                {
                    Console.WriteLine("{0} {1}", s.Name, s.Surname);
                }
            }
        }
Example #5
0
        static void Join(MyownContext context)
        {
            var result = from s in context.Students
                         join g in context.Groups
                         on s.GroupId equals g.Id
                         select new StudentViewModel
            {
                FullName  = s.Name + " " + s.Surname,
                Rating    = s.Rating,
                GroupName = g.Name
            };

            foreach (var s in result)
            {
                Console.WriteLine("{0} ({1}): rating = {2}", s.FullName, s.GroupName, s.Rating);
            }
        }