PageRenderTime 99ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/ztfuqingvip/mono
C# | 174 lines | 136 code | 37 blank | 1 comment | 14 complexity | f6c047f2f21ad311008485ad00c22877 MD5 | raw file
Possible License(s): GPL-2.0, Unlicense, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Test_NUnit;
  6. using NUnit.Framework;
  7. using Test_NUnit.Linq_101_Samples;
  8. #if MONO_STRICT
  9. using System.Data.Linq;
  10. #else
  11. using DbLinq.Data.Linq;
  12. #endif
  13. using nwind;
  14. // test ns Linq_101_Samples
  15. #if MYSQL
  16. namespace Test_NUnit_MySql.Linq_101_Samples
  17. #elif ORACLE && ODP
  18. namespace Test_NUnit_OracleODP.Linq_101_Samples
  19. #elif ORACLE
  20. namespace Test_NUnit_Oracle.Linq_101_Samples
  21. #elif POSTGRES
  22. namespace Test_NUnit_PostgreSql.Linq_101_Samples
  23. #elif SQLITE
  24. namespace Test_NUnit_Sqlite.Linq_101_Samples
  25. #elif INGRES
  26. namespace Test_NUnit_Ingres.Linq_101_Samples
  27. #elif MSSQL && MONO_STRICT
  28. namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
  29. #elif MSSQL
  30. namespace Test_NUnit_MsSql.Linq_101_Samples
  31. #elif FIREBIRD
  32. namespace Test_NUnit_Firebird.Linq_101_Samples
  33. #endif
  34. {
  35. [TestFixture]
  36. public class Object_Loading : TestBase
  37. {
  38. [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.")]
  39. public void LinqToSqlObject01()
  40. {
  41. Northwind db = CreateDB();
  42. var custs = from c in db.Customers
  43. where c.City == "Marseille"
  44. select c;
  45. foreach (var cust in custs)
  46. foreach (var ord in cust.Orders)
  47. {
  48. Console.WriteLine("CustomerID {0} has an OrderID {1}.", cust.CustomerID, ord.OrderID);
  49. }
  50. var list = custs.ToList();
  51. Assert.IsTrue(list.Count > 0);
  52. }
  53. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !MONO_STRICT))
  54. [Explicit]
  55. #endif
  56. [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
  57. [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.")]
  58. public void LinqToSqlObject02()
  59. {
  60. Northwind db = CreateDB();
  61. var ds = new DataLoadOptions();
  62. ds.LoadWith<Customer>(p => p.Orders);
  63. db.LoadOptions = ds;
  64. var custs = from c in db.Customers
  65. where c.City == "Marseille"
  66. select c;
  67. foreach (var cust in custs)
  68. foreach (var ord in cust.Orders)
  69. Console.WriteLine("CustomerID {0} has an OrderID {1}.", cust.CustomerID, ord.OrderID);
  70. var list = custs.ToList();
  71. Assert.IsTrue(list.Count > 0);
  72. }
  73. [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.")]
  74. public void LinqToSqlObject03()
  75. {
  76. Northwind db = CreateDB();
  77. var custs = from c in db.Customers
  78. where c.City == "London"
  79. select c;
  80. foreach (var cust in custs)
  81. foreach (var ord in cust.Orders)
  82. foreach (var orderDetail in ord.OrderDetails)
  83. {
  84. Console.WriteLine("CustomerID {0} has an OrderID {1} with ProductID {2} that has name {3}.",
  85. cust.CustomerID, ord.OrderID, orderDetail.ProductID, orderDetail.Product.ProductName);
  86. }
  87. var list = custs.ToList();
  88. Assert.IsTrue(list.Count > 0);
  89. }
  90. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !MONO_STRICT))
  91. [Explicit]
  92. #endif
  93. [Linq101SamplesModified("The original sample didn't compile, db2 Northwind context was used for nothing")]
  94. [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.")]
  95. public void LinqToSqlObject04()
  96. {
  97. var db = CreateDB();
  98. var ds = new DataLoadOptions();
  99. ds.LoadWith<Customer>(p => p.Orders);
  100. ds.LoadWith<Order>(p => p.OrderDetails);
  101. db.LoadOptions = ds;
  102. var custs = from c in db.Customers
  103. where c.City == "London"
  104. select c;
  105. foreach (var cust in custs)
  106. foreach (var ord in cust.Orders)
  107. foreach (var orderDetail in ord.OrderDetails)
  108. {
  109. Console.WriteLine("CustomerID {0} has an OrderID {1} with ProductID {2} that has name {3}.",
  110. cust.CustomerID, ord.OrderID, orderDetail.ProductID, orderDetail.Product.ProductName);
  111. }
  112. var list = custs.ToList();
  113. Assert.IsTrue(list.Count > 0);
  114. }
  115. [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.")]
  116. public void LinqToSqlObject05()
  117. {
  118. var db = CreateDB();
  119. var emps = from e in db.Employees select e;
  120. foreach (var emp in emps)
  121. foreach (var man in emp.Employees)
  122. Console.WriteLine("Employee {0} reported to Manager {1}.", emp.FirstName, man.FirstName);
  123. var list = emps.ToList();
  124. Assert.IsTrue(list.Count > 0);
  125. }
  126. [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.")]
  127. public void LinqToSqlObject06()
  128. {
  129. var db = CreateDB();
  130. var emps = from c in db.Employees select c;
  131. foreach (var emp in emps)
  132. Console.WriteLine("{0}", emp.Notes);
  133. var list = emps.ToList();
  134. Assert.IsTrue(list.Count > 0);
  135. }
  136. }
  137. }