PageRenderTime 37ms CodeModel.GetById 25ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/mcs/class/System.Data.Linq/src/DbLinq/Test/Providers/Linq_101_Samples/Object Loading.cs

https://bitbucket.org/danipen/mono
C# | 174 lines | 136 code | 37 blank | 1 comment | 14 complexity | f6c047f2f21ad311008485ad00c22877 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using Test_NUnit;
  6using NUnit.Framework;
  7using Test_NUnit.Linq_101_Samples;
  8
  9#if MONO_STRICT
 10using System.Data.Linq;
 11#else
 12using DbLinq.Data.Linq;
 13#endif
 14
 15using nwind;
 16
 17// test ns Linq_101_Samples
 18#if MYSQL
 19    namespace Test_NUnit_MySql.Linq_101_Samples
 20#elif ORACLE && ODP
 21    namespace Test_NUnit_OracleODP.Linq_101_Samples
 22#elif ORACLE
 23    namespace Test_NUnit_Oracle.Linq_101_Samples
 24#elif POSTGRES
 25    namespace Test_NUnit_PostgreSql.Linq_101_Samples
 26#elif SQLITE
 27    namespace Test_NUnit_Sqlite.Linq_101_Samples
 28#elif INGRES
 29    namespace Test_NUnit_Ingres.Linq_101_Samples
 30#elif MSSQL && MONO_STRICT
 31    namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
 32#elif MSSQL
 33    namespace Test_NUnit_MsSql.Linq_101_Samples
 34#elif FIREBIRD
 35    namespace Test_NUnit_Firebird.Linq_101_Samples
 36#endif
 37{
 38    [TestFixture]
 39    public class Object_Loading : TestBase
 40    {
 41        [Test(Description = "This sample demonstrates how to use Including to request related data during the original query so that additional roundtrips to the database are not required later when navigating through the retrieved objects.")]
 42        public void LinqToSqlObject01()
 43        {
 44            Northwind db = CreateDB();
 45
 46            var custs = from c in db.Customers
 47                        where c.City == "Marseille"
 48                        select c;
 49
 50            foreach (var cust in custs)
 51                foreach (var ord in cust.Orders)
 52                {
 53                    Console.WriteLine("CustomerID {0} has an OrderID {1}.", cust.CustomerID, ord.OrderID);
 54                }
 55
 56            var list = custs.ToList();
 57            Assert.IsTrue(list.Count > 0);
 58
 59        }
 60
 61#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !MONO_STRICT))
 62        [Explicit]
 63#endif
 64        [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
 65        [Test(Description = "This sample demonstrates how to use Including to request related data during the original query so that additional roundtrips to the database are not required later when navigating through the retrieved objects.")]
 66        public void LinqToSqlObject02()
 67        {
 68            Northwind db = CreateDB();
 69
 70
 71            var ds = new DataLoadOptions();
 72            ds.LoadWith<Customer>(p => p.Orders);
 73
 74            db.LoadOptions = ds;
 75
 76            var custs = from c in db.Customers
 77                        where c.City == "Marseille"
 78                        select c;
 79
 80            foreach (var cust in custs)
 81                foreach (var ord in cust.Orders)
 82                    Console.WriteLine("CustomerID {0} has an OrderID {1}.", cust.CustomerID, ord.OrderID);
 83
 84            var list = custs.ToList();
 85            Assert.IsTrue(list.Count > 0);
 86        }
 87
 88        [Test(Description = "This sample demonstrates how navigating through relationships in retrieved objects can end up triggering new queries to the database if the data was not requested by the original query.")]
 89        public void LinqToSqlObject03()
 90        {
 91            Northwind db = CreateDB();
 92
 93            var custs = from c in db.Customers
 94                        where c.City == "London"
 95                        select c;
 96
 97            foreach (var cust in custs)
 98                foreach (var ord in cust.Orders)
 99                    foreach (var orderDetail in ord.OrderDetails)
100                    {
101                        Console.WriteLine("CustomerID {0} has an OrderID {1} with ProductID {2} that has name {3}.",
102                            cust.CustomerID, ord.OrderID, orderDetail.ProductID, orderDetail.Product.ProductName);
103                    }
104
105            var list = custs.ToList();
106            Assert.IsTrue(list.Count > 0);
107
108        }
109
110#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !MONO_STRICT))
111        [Explicit]
112#endif
113        [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
114        [Test(Description = "This sample demonstrates how to use Including to request related data during the original query so that additional roundtrips to the database are not required later when navigating through the retrieved objects.")]
115        public void LinqToSqlObject04()
116        {
117            var db = CreateDB();
118
119            var ds = new DataLoadOptions();
120            ds.LoadWith<Customer>(p => p.Orders);
121            ds.LoadWith<Order>(p => p.OrderDetails);
122
123            db.LoadOptions = ds;
124
125            var custs = from c in db.Customers
126                        where c.City == "London"
127                        select c;
128
129            foreach (var cust in custs)
130                foreach (var ord in cust.Orders)
131                    foreach (var orderDetail in ord.OrderDetails)
132                    {
133                        Console.WriteLine("CustomerID {0} has an OrderID {1} with ProductID {2} that has name {3}.",
134                            cust.CustomerID, ord.OrderID, orderDetail.ProductID, orderDetail.Product.ProductName);
135                    }
136
137            var list = custs.ToList();
138            Assert.IsTrue(list.Count > 0);
139
140
141        }
142
143        [Test(Description = "This sample demonstrates how navigating through relationships in retrieved objects can result in triggering new queries to the database if the data was not requested by the original query.")]
144        public void LinqToSqlObject05()
145        {
146            var db = CreateDB();
147
148            var emps = from e in db.Employees select e;
149
150            foreach (var emp in emps)
151                foreach (var man in emp.Employees)
152                    Console.WriteLine("Employee {0} reported to Manager {1}.", emp.FirstName, man.FirstName);
153
154            var list = emps.ToList();
155            Assert.IsTrue(list.Count > 0);
156        }
157
158
159
160        [Test(Description = "This sample demonstrates how navigating through Link in retrieved objects can end up triggering new queries to the database if the data type is Link.")]
161        public void LinqToSqlObject06()
162        {
163            var db = CreateDB();
164
165            var emps = from c in db.Employees select c;
166
167            foreach (var emp in emps)
168                Console.WriteLine("{0}", emp.Notes);
169
170            var list = emps.ToList();
171            Assert.IsTrue(list.Count > 0);
172        }
173    }
174}