5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
31 using NUnit.Framework;
\r
33 using Test_NUnit.Linq_101_Samples;
\r
37 // test ns Linq_101_Samples
\r
39 namespace Test_NUnit_MySql.Linq_101_Samples
\r
41 namespace Test_NUnit_OracleODP.Linq_101_Samples
\r
43 namespace Test_NUnit_Oracle.Linq_101_Samples
\r
45 namespace Test_NUnit_PostgreSql.Linq_101_Samples
\r
47 namespace Test_NUnit_Sqlite.Linq_101_Samples
\r
49 namespace Test_NUnit_Ingres.Linq_101_Samples
\r
50 #elif MSSQL && L2SQL
\r
51 namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
\r
53 namespace Test_NUnit_MsSql.Linq_101_Samples
\r
55 namespace Test_NUnit_Firebird.Linq_101_Samples
\r
59 public class Conversion_Operators : TestBase
\r
61 [Test(Description = "AsEnumerable.This sample uses ToArray so that the client-side IEnumerable(Of T) implementation of where is used, instead of the default Query(Of T) implementation which would be converted to SQL and executed on the server. This is necessary because the where clause references a user-defined client-side method, isValidProduct, which cannot be converted to SQL.")]
\r
62 public void LinqToSqlConversions01()
\r
64 Northwind db = CreateDB();
\r
66 var q = from p in db.Products.AsEnumerable()
\r
67 where isValidProduct(p)
\r
70 var list = q.ToList();
\r
71 Assert.IsTrue(list.Count > 0);
\r
75 private bool isValidProduct(Product p)
\r
77 return (p.ProductName.LastIndexOf("C") == 0);
\r
81 [Test(Description = "ToArray. This sample uses ToArray to immediately evaluate a query into an array and get the 3rd element.")]
\r
82 public void LinqToSqlConversions02()
\r
84 Northwind db = CreateDB();
\r
86 var q = from c in db.Customers
\r
87 where c.City == "London"
\r
90 Customer[] list = q.ToArray();
\r
92 Assert.IsFalse(list == null);
\r
93 Assert.IsTrue(list.Length > 0);
\r
96 #if !DEBUG && SQLITE
\r
99 [Test(Description = "ToList. This sample uses ToList to immediately evaluate a query into a List(Of T).")]
\r
100 public void LinqToSqlConversions03()
\r
102 Northwind db = CreateDB();
\r
104 var q = from e in db.Employees
\r
105 where e.HireDate >= DateTime.Parse("1/1/1994")
\r
108 List<Employee> qList = q.ToList();
\r
110 Assert.IsFalse(qList == null);
\r
111 Assert.IsTrue(qList.Count > 0);
\r
114 #if !DEBUG && (MSSQL && !L2SQL)
\r
117 [Linq101SamplesModified("Strange short to boolean casting, perhaps in the original Northwind Product.Discontinued was a boolean property")]
\r
118 [Test(Description = "ToDictionary. This sample uses ToDictionary to immediately evaluate a query and a key expression into an Dictionary(Of K, T).")]
\r
119 public void LinqToSqlConversion04()
\r
121 Northwind db = CreateDB();
\r
123 var q = from p in db.Products
\r
124 where p.UnitsInStock <= p.ReorderLevel && !Convert.ToBoolean(p.Discontinued)
\r
127 var qDictionary = q.ToDictionary(p => p.ProductID);
\r
129 Assert.IsFalse(qDictionary == null);
\r
130 // PC: on SQLite, this returns nothing. Is the test wrong?
\r
131 if (qDictionary.Count == 0)
\r
132 Assert.Ignore("Please check this test validity");
\r
133 //Assert.IsTrue(qDictionary.Count > 0);
\r
135 foreach (var key in qDictionary.Keys)
\r
137 Console.WriteLine("Key {0}:", key);
\r
138 Console.WriteLine();
\r