File: Count_Sum_Min_Max_Avg.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (271 lines) | stat: -rw-r--r-- 12,066 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Test_NUnit;

using nwind;

// test ns Linq_101_Samples
#if MYSQL
    namespace Test_NUnit_MySql.Linq_101_Samples
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP.Linq_101_Samples
#elif ORACLE
    namespace Test_NUnit_Oracle.Linq_101_Samples
#elif POSTGRES
    namespace Test_NUnit_PostgreSql.Linq_101_Samples
#elif SQLITE
    namespace Test_NUnit_Sqlite.Linq_101_Samples
#elif INGRES
    namespace Test_NUnit_Ingres.Linq_101_Samples
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
#elif MSSQL
    namespace Test_NUnit_MsSql.Linq_101_Samples
#elif FIREBIRD
    namespace Test_NUnit_Firebird.Linq_101_Samples
#endif
{
    /// <summary>
    /// Source:  http://msdn2.microsoft.com/en-us/vbasic/bb737922.aspx
    /// manually translated from VB into C#.
    /// </summary>
    [TestFixture]
    public class Count_Sum_Min_Max_Avg : TestBase
    {
        [Test]
        public void LinqToSqlCount01()
        {
            Northwind db = CreateDB();
            var q = db.Customers.Count();

            Assert.IsTrue(q > 0, "Expect non-zero count");
        }

#if !DEBUG && (MSSQL && !L2SQL)
        [Explicit]
#endif
        [Test]
        public void LinqToSqlCount02()
        {
            Northwind db = CreateDB();
#if INGRES && !MONO_STRICT
            var q = (from p in db.Products where p.Discontinued == "N" select p)
                .Count();
#else
            var q = (from p in db.Products where !p.Discontinued select p)
                .Count();
#endif

            Assert.IsTrue(q > 0, "Expect non-zero count");
        }

        [Test(Description = "This sample uses Sum to find the total freight over all Orders.")]
        public void LinqToSqlCount03()
        {
            Northwind db = CreateDB();
            var q = (from o in db.Orders select o.Freight).Sum();
            Assert.IsTrue(q > 0, "Freight sum must be > 0");
        }

        [Test(Description = "This sample uses Sum to find the total number of units on order over all Products.")]
        public void LinqToSqlCount04()
        {
            Northwind db = CreateDB();
            var q = (from p in db.Products select (int)p.UnitsOnOrder.Value).Sum();
            Assert.IsTrue(q > 0, "Freight sum must be > 0");
        }

        [Test(Description = "This sample uses Min to find the lowest unit price of any Product")]
        public void LinqToSqlCount05()
        {
            Northwind db = CreateDB();
            var q = (from p in db.Products select p.UnitsOnOrder).Min();
            Assert.IsTrue(q == 0, "Min UnitsOnOrder must be 0");
        }

        [Test(Description = "This sample uses Min to find the lowest freight of any Order.")]
        public void LinqToSqlCount06()
        {
            Northwind db = CreateDB();
            var q = (from o in db.Orders select o.Freight).Min();
            Assert.IsTrue(q > 0, "Freight sum must be > 0");
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test(Description = "This sample uses Min to find the Products that have the lowest unit price in each category")]
        public void LinqToSqlCount07()
        {
            #region SHOW_MICROSOFT_GENERATED_SQL
            /*
            //the one Linq statement below gets translated into 9 SQL statements
SELECT MIN([t0].[UnitPrice]) AS [value], [t0].[CategoryID]
FROM [dbo].[Products] AS [t0]
GROUP BY [t0].[CategoryID]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [4.5000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [3]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [9.2000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [4]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [2.5000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [5]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [7.0000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [6]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [7.4500]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [7]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000]

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [8]
-- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [6.0000]
    */
            #endregion

            Northwind db = CreateDB();
            var categories = (from p in db.Products
                              group p by p.CategoryID into g
                              select new
                              {
                                  CategoryID = g.Key,
                                  CheapestProducts = from p2 in g
                                                     where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
                                                     select p2
                              });

            var list = categories.ToList();
            Assert.IsTrue(list.Count > 0, "Expected count > 0");
        }

        [Test(Description = "This sample uses Max to find the latest hire date of any Employee")]
        public void LinqToSqlCount08()
        {
            Northwind db = CreateDB();
            var q = (from e in db.Employees select e.HireDate).Max();
            Assert.IsTrue(q > new DateTime(1990, 1, 1), "Hire date must be > 2000");
        }

        [Test(Description = "This sample uses Max to find the most units in stock of any Product")]
        public void LinqToSqlCount09()
        {
            Northwind db = CreateDB();
            var q = (from p in db.Products select p.UnitsInStock).Max();
            Assert.IsTrue(q > 0, "Max UnitsInStock must be > 0");
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test(Description = "This sample uses Max to find the Products that have the highest unit price in each category")]
        public void LinqToSqlCount10()
        {
            //Miscrosoft translates this query into multiple SQL statements
            Northwind db = CreateDB();
            var q = from p in db.Products
                    group p by p.CategoryID into g
                    select new
                    {
                        g,
                        MostExpensiveProducts = from p2 in g
                                                where p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
                                                select p2
                    };
            var list = q.ToList();
            Assert.IsTrue(list.Count > 0, "Got most expensive items > 0");
        }



#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test(Description = "This sample uses Average to find the average freight of all Orders.")]
        public void LinqToSqlCount11()
        {
            Northwind db = CreateDB();
            var q = (from o in db.Orders
                     select o.Freight).Average();

            Console.WriteLine(q);
            Assert.IsTrue(q > 0, "Avg orders'freight must be > 0");
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test(Description = "This sample uses Average to find the average unit price of all Products.")]
        public void LinqToSqlCount12()
        {
            Northwind db = CreateDB();
            var q = (from p in db.Products
                     select p.UnitPrice).Average();

            Console.WriteLine(q);

            Console.WriteLine(q);
            Assert.IsTrue(q > 0, "Avg products'unitPrice must be > 0");
        }


#if !INGRES
#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test(Description = "This sample uses Average to find the Products that have unit price higher than the average unit price of the category for each category.")]
        public void LinqToSqlCount13()
        {
            Northwind db = CreateDB();
            var categories = from p in db.Products
                             group p by p.CategoryID into g
                             select new
                                {
                                    g,
                                    ExpensiveProducts = from p2 in g
                                                        where (p2.UnitPrice > g.Average(p3 => p3.UnitPrice))
                                                        select p2
                                };


            var list = categories.ToList();
            Assert.IsTrue(list.Count > 0, "Got categorized products > 0");
        }
#endif


    }
}