示例#1
0
        static void CopyToDatabase(Context1 context)
        {
            Repository repo = new Repository();

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

            context.SaveChanges();
        }
示例#2
0
        static void FilterAndOrder(Context1 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);
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            using (Context1 c = new Context1())
            {
                // Uncomment and run once to fill the database
                // CopyToDatabase(c);

                // Queries
                //FilterAndOrder(c);
                //GroupByFirstLetter(c);
                Join(c);
            }
            Console.ReadKey();
        }
示例#4
0
        static void GroupByFirstLetter(Context1 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);
                }
            }
        }
示例#5
0
        static void Join(Context1 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);
            }
        }