Пример #1
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var hotel = new Hotel {
                    Name = "Five Seasons Resort"
                };
                var v1 = new Visitor {
                    Name = "Alex Stevens"
                };
                var v2 = new Visitor {
                    Name = "Joan Hills"
                };
                var r1 = new Reservation {
                    Cost = 79.99M, Hotel = hotel, ReservationDate = DateTime.Parse("2/19/2010"), Visitor = v1
                };
                var r2 = new Reservation {
                    Cost = 99.99M, Hotel = hotel, ReservationDate = DateTime.Parse("2/17/2010"), Visitor = v2
                };
                var r3 = new Reservation {
                    Cost = 109.99M, Hotel = hotel, ReservationDate = DateTime.Parse("2/18/2010"), Visitor = v1
                };
                var r4 = new Reservation {
                    Cost = 89.99M, Hotel = hotel, ReservationDate = DateTime.Parse("2/17/2010"), Visitor = v2
                };
                context.Hotels.AddObject(hotel);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Using eSql...");
                var esql     = @"Select value v from EFRecipesModel.VisitorSummary(DATETIME'2010-02-16 00:00', 7) as v";
                var visitors = context.CreateQuery <DbDataRecord>(esql);
                foreach (var visitor in visitors)
                {
                    Console.WriteLine("{0}, Total Reservations: {1}, Revenue: {2:C}",
                                      visitor["Name"], visitor["TotalReservations"], visitor["BusinessEarned"]);
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine();
                Console.WriteLine("Using LINQ...");
                var visitors = from v in context.VisitorSummary(DateTime.Parse("2/16/2010"), 7)
                               select v;
                foreach (var visitor in visitors)
                {
                    Console.WriteLine("{0}, Total Reservations: {1}, Revenue: {2:C}",
                                      visitor["Name"], visitor["TotalReservations"], visitor["BusinessEarned"]);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Пример #2
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var post1 = new BlogPost {
                    Title = "The Joy of LINQ", Description = "101 things you always wanted to know about LINQ"
                };
                var post2 = new BlogPost {
                    Title = "LINQ as Dinner Conversation", Description = "What wine goes with a Lambda expression?"
                };
                var post3 = new BlogPost {
                    Title = "LINQ and our Children", Description = "Why we need to teach LINQ in High School"
                };
                var comment1 = new Comment {
                    Comments = "Great post, I wish more people would talk about LINQ"
                };
                var comment2 = new Comment {
                    Comments = "You're right, we should teach LINQ in high school!"
                };
                post1.Comments.Add(comment1);
                post3.Comments.Add(comment2);
                context.BlogPosts.AddObject(post1);
                context.BlogPosts.AddObject(post2);
                context.BlogPosts.AddObject(post3);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Blog Posts with comments...(LINQ)");
                var posts = from post in context.BlogPosts
                            where post.Comments.Any()
                            select post;
                foreach (var post in posts)
                {
                    Console.WriteLine("Blog Post: {0}", post.Title);
                    foreach (var comment in post.Comments)
                    {
                        Console.WriteLine("\t{0}", comment.Comments);
                    }
                }
            }
            Console.WriteLine();

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Blog Posts with comments...(ESQL)");
                var esql  = "select value p from BlogPosts as p where exists(p.Comments)";
                var posts = context.CreateQuery <BlogPost>(esql);
                foreach (var post in posts)
                {
                    Console.WriteLine("Blog Post: {0}", post.Title);
                    foreach (var comment in post.Comments)
                    {
                        Console.WriteLine("\t{0}", comment.Comments);
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }