コード例 #1
0
        private static void Joins()
        {
            using (var context = new SampleEntities())
            {
                var joinQuery = from c in context.Contacts
                                join oa in context.vOfficeAddresses on c.ContactID equals oa.ContactID
                                select new
                {
                    oa.FirstName,
                    oa.LastName,
                    c.Title,
                    oa.Street1,
                    oa.City,
                    oa.StateProvince
                };

                var nestedQuery = context.vOfficeAddresses.Select(oa => new
                {
                    oa.FirstName,
                    oa.LastName,
                    Title = context.Contacts.Where(c => c.ContactID == oa.ContactID).Select(c => c.Title).FirstOrDefault(),
                    oa.Street1,
                    oa.City,
                    oa.StateProvince
                });
            }
        }
コード例 #2
0
 private static void Edit()
 {
     using (var context = new SampleEntities())
     {
         var contact = context.Contacts.First();
         contact.FirstName    = "Julia";
         contact.ModifiedDate = DateTime.Now;
         context.SaveChanges();
     }
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: ryukocn2008/FinTech
 private static void QueryContacts()
 {
     using (var context = new SampleEntities())
     {
         var contacts = context.Contacts;
         foreach (var contact in contacts)
         {
             Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
         }
     }
 }
コード例 #4
0
        private static void VariousMethods()
        {
            using (var context = new SampleEntities())
            {
                var contacts = context.Contacts.Where(c => c.FirstName == "Robert");

                var list = contacts.ToList();
                Console.WriteLine(contacts.ToString());
                Console.ReadLine();
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: ryukocn2008/FinTech
        private static void QueryLambda()
        {
            var context  = new SampleEntities();
            var contacts = context.Contacts
                           .Where(c => c.FirstName == "Robert");

            foreach (var contact in contacts)
            {
                Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
            }
        }
コード例 #6
0
 private static void Grouping()
 {
     using (var context = new SampleEntities())
     {
         var groupQuery = from c in context.Contacts
                          group c by c.Title
                          into myGroup
                          orderby myGroup.Key
                          select new { MyTitle = myGroup.Key, MyGroup = myGroup };
     }
 }
コード例 #7
0
ファイル: Program.cs プロジェクト: ryukocn2008/FinTech
        private static void QueryLINQ()
        {
            var context  = new SampleEntities();
            var contacts = from c in context.Contacts
                           where c.FirstName == "Robert"
                           select c;

            foreach (var contact in contacts)
            {
                Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
            }
        }
コード例 #8
0
 private static void QueryContactsLambda()
 {
     using (var context = new SampleEntities())
     {
         var contacts = context.Contacts.Where(c => c.FirstName == "Robert").OrderBy(foo => foo.LastName);
         foreach (var contact in contacts)
         {
             Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName}");
         }
         Console.WriteLine("Perss Enter...");
         Console.ReadLine();
     }
 }
コード例 #9
0
 private static void QueryContacts()
 {
     using (var context = new SampleEntities())
     {
         var contacts = context.Contacts;
         foreach (var contact in contacts)
         {
             Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName}");
         }
         Console.WriteLine("Perss Enter...");
         Console.ReadLine();
     }
 }
コード例 #10
0
        private static void ExecuteOption()
        {
            using (var context = new SampleEntities())
            {
                var list = context.ContactsObjectSet.Execute(MergeOption.NoTracking).ToList();

                foreach (var address in list)
                {
                    Console.WriteLine(address.FirstName);
                }
                Console.ReadLine();
            }
        }
コード例 #11
0
 private static void LinqProjections()
 {
     using (var context = new SampleEntities())
     {
         var contacts = context.Contacts.Where(c => c.FirstName == "Robert").OrderBy(foo => foo.LastName).Select(c => new { c.Title, c.FirstName, c.LastName });
         foreach (var contact in contacts)
         {
             Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName}");
         }
         Console.WriteLine("Perss Enter...");
         Console.ReadLine();
     }
 }
コード例 #12
0
 private static void EagerLoading()
 {
     using (var context = new SampleEntities())
     {
         var contacts = context.Contacts.Include(a => a.Addresses).Select(c => c).ToList();
         foreach (var contact in contacts)
         {
             Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName} {contact.Addresses.Count}");
         }
         Console.WriteLine("Perss Enter from eager...");
         Console.ReadLine();
     }
 }
コード例 #13
0
ファイル: Program.cs プロジェクト: ryukocn2008/FinTech
        private static void QueryEntityQuery()
        {
            var context = new SampleEntities();
            var query   = "SELECT VALUE c " +
                          "FROM SampleEntities.Contacts AS c " +
                          "WHERE c.FirstName = 'Robert'";

            ObjectQuery <Contact> contacts = context.CreateQuery <Contact>(query);

            foreach (var contact in contacts)
            {
                Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
            }
        }
コード例 #14
0
        private static void RemoveEnitiy()
        {
            using (var context = new SampleEntities())
            {
                var contact = context.Contacts.Find(438);

                if (contact != null)
                {
                    context.Contacts.Remove(contact);
                }

                context.SaveChanges();
            }
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: shin820/ProgrammingEF
        private static void QueryContacts()
        {
            using (var context = new SampleEntities())
            {
                var contacts = from c in context.Contacts select c;

                foreach (var contact in contacts)
                {
                    Console.WriteLine(contact.FirstName + " " + contact.LastName);
                }

                Console.Read();
            }
        }
コード例 #16
0
 private static void ChainingAggregates()
 {
     using (var context = new SampleEntities())
     {
         var chainingAggregatesQuery =
             context.Contacts.GroupBy(c => c.Title).OrderBy(myGroup => myGroup.Key).Select(myGroup => new
         {
             MyTitle = myGroup.Key,
             MyGroup = myGroup,
             Max     = myGroup.Max(c => c.AddDate),
             Count   = myGroup.Count()
         });
     }
 }
コード例 #17
0
 private static void QueryContactsLinq()
 {
     using (var context = new SampleEntities())
     {
         var contacts = from c in context.Contacts
                        where c.FirstName == "Robert"
                        select c;
         foreach (var contact in contacts)
         {
             Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName}");
         }
         Console.WriteLine("Perss Enter...");
         Console.ReadLine();
     }
 }
コード例 #18
0
        private static void CreateParentChildEntity()
        {
            using (var context = new SampleEntities())
            {
                var contact = new Contact
                {
                    FirstName    = "Camey",
                    LastName     = "Combs",
                    AddDate      = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    Addresses    = new List <Address>()
                };
                var address = new Address
                {
                    Street1       = "One Main Street",
                    City          = "Olympia",
                    StateProvince = "WA",
                    AddressType   = "Business",
                    ModifiedDate  = DateTime.Now,
                    Contact       = contact
                };

                var contact1 = new Contact
                {
                    FirstName    = "Camey",
                    LastName     = "Combs",
                    AddDate      = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    Addresses    = new List <Address> {
                        new Address
                        {
                            Street1       = "One Main Street",
                            City          = "Olympia",
                            StateProvince = "WA",
                            AddressType   = "Business",
                            ModifiedDate  = DateTime.Now
                        }
                    }
                };
                context.Contacts.Add(contact1);

                contact.Addresses.Add(address);
                //join the new address to the contact
                //add the new graph to the context
                context.Contacts.Add(contact);
                context.SaveChanges();
            }
        }
コード例 #19
0
        private static void QueryContacts()
        {
            using (var context = new SampleEntities())
            {
                IQueryable <Contact> contacts = context.Contacts.Where(a => a.LastName.StartsWith("A"));

                if (!Object.ReferenceEquals(contacts, null))
                {
                    foreach (var contact in contacts)
                    {
                        Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
                    }
                }
            }
            Console.ReadLine();
        }
コード例 #20
0
        private static void MergeOptionExampleObjectSet()
        {
            using (var context = new SampleEntities())
            {
                var contactsQuery = context.ContactsObjectSet.Where(c => c.FirstName == "Robert");
                ((ObjectQuery)contactsQuery).MergeOption = MergeOption.PreserveChanges;
                var results = contactsQuery.ToList();

                foreach (var address in results)
                {
                    Console.WriteLine(address.FirstName);
                }

                Console.ReadLine();
            }
        }
コード例 #21
0
        private static void MergeOptionExample()
        {
            using (var context = new SampleEntities())
            {
                var ctx = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet <Contact>();

                var contactsQuery = ctx.Select(m => m);
                ((ObjectQuery)contactsQuery).MergeOption = MergeOption.NoTracking;
                var list = contactsQuery.ToList();

                foreach (var address in list)
                {
                    Console.WriteLine(address.FirstName);
                }

                Console.ReadLine();
            }
        }
コード例 #22
0
        private static void CreateEntity()
        {
            using (var context = new SampleEntities())
            {
                var contact = context.Contacts.First(c => c.FirstName == "Robert");
                var address = new Address
                {
                    Street1       = "One Main Street",
                    City          = "Burlington",
                    StateProvince = "VT",
                    AddressType   = "Business",
                    ModifiedDate  = DateTime.Now,
                    Contact       = contact
                };
                contact.Addresses.Add(address);

                //join the new address to the contact
                context.SaveChanges();
            }
        }
コード例 #23
0
        private static void LetProjectionWithLinq()
        {
            using (var context = new SampleEntities())
            {
                var contacts = from c in context.Contacts
                               where c.FirstName == "Robert"
                               let contactName = new { c.Title, c.LastName, c.FirstName }
                select contactName;

                foreach (var contact in contacts)
                {
                    Console.WriteLine($"{contact.FirstName.Trim()} {contact.LastName}");
                }

                var contacts1 =
                    from c in context.Contacts
                    where c.FirstName == "Robert"
                    let foo = new
                {
                    ContactName = new { c.Title, c.LastName, c.FirstName },
                    c.Addresses     //this participates in change tracking and DB updates
                }
                orderby foo.ContactName.LastName
                select foo;

                foreach (var contact in contacts1)
                {
                    var name = contact.ContactName;
                    Console.WriteLine(
                        $"{name.Title.Trim()} {name.FirstName.Trim()} {name.LastName.Trim()}: # Addresses {contact.Addresses.Count}");
                }

                var addresses = context.Addresses.Where(a => a.CountryRegion == "UK").Select(a => new
                {
                    a,
                    a.Contact.FirstName,
                    a.Contact.LastName
                });

                foreach (var address in addresses)
                {
                    Console.WriteLine($"{address.FirstName} {address.LastName} {address.a.Street1} {address.a.City}");
                }

                var shapedResults = from c in context.Contacts
                                    select new
                {
                    c.FirstName,
                    c.LastName,
                    StreetsCities = from a in c.Addresses
                                    select new { a.Street1, a.City }
                };

                var shapedResultsLinq = context.Contacts.Select(c => new
                {
                    c.FirstName,
                    c.LastName,
                    StreetsCities = c.Addresses.Select(a => new { a.Street1, a.City })
                });

                var flattenedResults =
                    from a in context.Addresses
                    orderby a.Contact.LastName
                    select new { a.Contact.LastName, a.Contact.FirstName, a.Street1, a.City };

                Console.WriteLine("Perss Enter...");
                Console.ReadLine();
            }
        }