Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSqlEntityConnectionExample1.fsx
More file actions
410 lines (323 loc) · 12.6 KB
/
Copy pathSqlEntityConnectionExample1.fsx
File metadata and controls
410 lines (323 loc) · 12.6 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#r "System.Data.Linq.dll"
#r "System.Data.dll"
#r "System.ComponentModel.DataAnnotations.dll"
#r "System.Data.Entity.dll"
#r "packages/FSharp.Data.TypeProviders/lib/net40/FSharp.Data.TypeProviders.dll"
#load "vizlib/show.fsx"
open System
open System.Data.Common
open System.ComponentModel.DataAnnotations
open System.Data.EntityClient
open System.Data.SqlClient
open System.Data.Sql
open System.Linq
open Microsoft.FSharp.Linq
open FSharp.Data.TypeProviders
type NorthwndDb =
SqlEntityConnection<ConnectionString = const (@"AttachDBFileName = '" + __SOURCE_DIRECTORY__ + @"\data\northwnd.mdf';Server='.\SQLEXPRESS';User Instance=true;;Integrated Security=SSPI"),Pluralize=true>
let db = NorthwndDb.GetDataContext()
//-------------------- BASIC QUERIES --------------------------------------------
let customers =
query { for c in db.Customers do
select c }
customers |> showGrid
//-------------------- BASIC QUERIES (task) --------------------------------------------
// Adjust the query to return the contact names for the customers
//
// let customerContactNames =
// query { for c in db.Customers do
// select c }
customers |> showGrid
//-------------------- FILTERS etc. --------------------------------------------
//
let customersWithNamesStartingWithB =
query { for c in db.Customers do
where (c.ContactName.StartsWith "B") }
|> teeGrid
//-------------------- FILTERS (task) --------------------------------------------
//
// Get the first 5 (first name, last name) pairs of employees born after 1960 whose last name starts with 'S'
let oldEmployeesWithLastNameStartingWithS =
query { for emp in db.Employees do
//where ...
//where ...
//select ...
//take 5
}
|> teeGrid
//-------------------- AGGREGATIONS etc. --------------------------------------------
// Note: C# uses '.Distinct()'
//
let distinctCompanyNames =
query { for c in db.Customers do
select c.CompanyName
distinct }
|> teeGrid
let customersAverageOrders =
query { for c in db.Customers do
averageBy (float c.Orders.Count) }
|> teeGrid
//-------------------- AGGREGATIONS (task) --------------------------------------------
// TASK: Get the sum of all orders for all customers
//
// You may need to use 'float' to convert an integer to a double.
let sumOfAllOrdersForCustomers =
query { for c in db.Customers do
sumBy ...
}
|> teeGrid
//-------------------- FURTHER EXAMPLE QUERIES --------------------------------------------
let employeesWithEmployees1 =
query { for c in db.Employees do
for x in c.Employees1 do
select (c.FirstName,c.LastName,x.City) }
|> teeGrid
let managersWithEmployeesInLondon1 =
query { for c in db.Employees do
where (query { for x in c.Employees1 do exists (x.City = "London") })
select c }
|> teeGrid
let managersWithEmployeesInLondon2 =
query { for c in db.Employees do
where (c.Employees1.Any (fun x -> x.City = "London"))
select c }
|> teeGrid
let customerWithNamesStartingWithB =
query { for c in db.Customers do
where (c.CompanyName.StartsWith "A")
select c }
|> teeGrid
let customerNames =
query { for c in db.Customers do
select c.ContactName }
|> teeGrid
let customersSortedByCountry =
query { for c in db.Customers do
sortBy c.Country
select (c.Country, c.ContactName) }
|> teeGrid
let res =
query { for emp in db.Employees do
where (emp.BirthDate.Value.Year > 1960)
where (emp.LastName.StartsWith "S")
select (emp.FirstName, emp.LastName)
take 5 }
|> teeGrid
let joinCustomersAndEmployeesByName =
query { for c in db.Customers do
join e in db.Employees on (c.Country = e.Country)
select (c.ContactName, e.LastName) }
|> teeGrid
let customersSortedDescending =
query { for c in db.Customers do
sortByDescending c.Country
select (c.Country, c.CompanyName) }
|> teeGrid
let customersSortedTwoColumns =
query { for c in db.Customers do
sortBy c.Country; thenBy c.Region
select (c.Country, c.Region, c.CompanyName) }
|> teeGrid
let customersSortedTwoColumnsAscendingDescending =
query { for c in db.Customers do
sortBy c.Country; thenByDescending c.Region
select (c.Country, c.Region, c.CompanyName) }
|> teeGrid
let sumOfAllOrdersForCustomers =
query { for c in db.Customers do
sumBy (float c.Orders.Count) }
|> teeGrid
let customersSortedTwoColumnsDescendingAscending =
query { for c in db.Customers do
sortByDescending c.Country; thenBy c.Region
select (c.Country, c.Region, c.CompanyName) }
|> teeGrid
let customerSpecificsSorted =
query { for c in db.Customers do
sortBy c.Country
select (c.Country, c.Region, c.CompanyName) }
|> teeGrid
let customerSpecificsSortedTwoColumns =
query { for c in db.Customers do
sortBy c.Country
thenBy c.Region
select (c.Country, c.Region, c.CompanyName) }
|> teeGrid
let customerLongestNameLength =
query { for c in db.Customers do
maxBy c.ContactName.Length }
|> teeGrid
let sumOfLengthsOfCustomerNames =
query { for c in db.Customers do
sumBy c.ContactName.Length }
|> teeGrid
let customersAtSpecificAddress =
query { for c in db.Customers do
where (c.Address.Contains("Jardim das rosas"))
select c }
|> teeGrid
let customersAtSpecificAddressUsingIf =
query { for c in db.Customers do
if (c.Address.Contains("Jardim das rosas")) then
select c }
|> teeGrid
let productsGroupedByName =
query { for p in db.Products do
groupBy p.ProductName }
|> Seq.map (fun g -> g.Key, seq { for p in g -> p.ProductName })
|> teeGrid
let productsGroupedByName2 =
query { for p in db.Products do
groupValBy p p.ProductName }
|> Seq.map (fun g -> g.Key, seq { for p in g -> p.ProductName })
|> teeGrid
let countOfAllUnitsInStockForAllProducts =
query { for p in db.Products do
sumBy (int p.UnitsInStock.Value) }
|> teeGrid
let sumByUsingValue =
// .Net SqlClient Data Provider: Warning: Null value is eliminated by an aggregate or other SET operation..
query { for p in db.Employees do
// This corresponds to SQL semantics if you assume SQL warnings are treated as errors
sumBy p.ReportsTo.Value }
|> teeGrid
let sumByNullableExample =
query { for p in db.Employees do
sumByNullable p.ReportsTo }
|> teeGrid
let namesAndIdsOfProductsGroupedByName =
query { for p in db.Products do
groupBy p.Category.CategoryName into group
for p in group do
select (group.Key, p.ProductName) }
|> teeGrid
let averagePriceOverProductRange =
query { for p in db.Products do
averageByNullable p.UnitPrice }
|> teeGrid
let totalOrderQuantity =
query { for c in db.Customers do
let numOrders =
query { for o in c.Orders do
for od in o.Order_Details do
sumByNullable (Nullable(int od.Quantity)) }
let averagePrice =
query { for o in c.Orders do
for od in o.Order_Details do
averageByNullable (Nullable(od.UnitPrice)) }
select (c.ContactName, numOrders, averagePrice) }
|> teeGrid
let productsGroupedByNameAndCountedTest1 =
query { for p in db.Products do
groupBy p.Category.CategoryName into group
let sum =
query { for p in group do
sumBy (int p.UnitsInStock.Value) }
select (group.Key, sum) }
|> teeGrid
let sumOfUnitsInStock =
query { for p in db.Products do
sumBy (int p.UnitsInStock.Value) }
|> teeGrid
let namesAndIdsOfProductsGroupdByID =
query { for p in db.Products do
groupBy p.CategoryID into group
for p in group do
select (group.Key, p.ProductName, p.ProductID) }
|> teeGrid
let minUnitPriceOfProductsGroupedByName =
query { for p in db.Products do
groupBy p.Category into group
let minOfGroup =
query { for p in group do
minByNullable p.UnitPrice }
select (group.Key.CategoryName, minOfGroup) }
|> teeGrid
let crossJoinOfCustomersAndEmployees =
query { for c in db.Customers do
for e in db.Employees do
select (c.CompanyName, e.LastName) }
|> teeGrid
let innerJoinQuery =
query { for c in db.Categories do
join p in db.Products on (c.CategoryID =? p.CategoryID)
select (p.ProductName, c.CategoryName) } //produces flat sequence
|> teeGrid
let joinCustomersAndEmployeesByNameUsingLoopAndConstraint =
query { for c in db.Customers do
for e in db.Employees do
where (c.Country = e.Country)
select (c.ContactName + " " + e.LastName) }
|> teeGrid
let innerJoinQueryUsingLoopAndConstraint =
query { for c in db.Categories do
for p in db.Products do
where (c.CategoryID =? p.CategoryID)
select (p.ProductName, c.CategoryName) } //produces flat sequence
|> teeGrid
let innerGroupJoinQuery =
query { for c in db.Categories do
groupJoin p in db.Products on (c.CategoryID =? p.CategoryID) into prodGroup
select (c.CategoryName, prodGroup) }
|> teeGrid
let innerGroupJoinQueryWithAggregation =
query { for c in db.Categories do
groupJoin p in db.Products on (c.CategoryID =? p.CategoryID) into prodGroup
let groupMax = query { for p in prodGroup do maxByNullable p.UnitsOnOrder }
select (c.CategoryName, groupMax) }
|> teeGrid
let innerGroupJoinQueryWithFollowingLoop =
query { for c in db.Categories do
groupJoin p in db.Products on (c.CategoryID =? p.CategoryID) into prodGroup
for prod2 in prodGroup do
where (prod2.UnitPrice ?> 2.50M)
select (c.CategoryName, prod2) }
|> teeGrid
let leftOuterJoinQuery =
query { for c in db.Categories do
groupJoin p in db.Products on (c.CategoryID =? p.CategoryID) into prodGroup
let prodGroup = System.Linq.Enumerable.DefaultIfEmpty prodGroup
for item in prodGroup do
select (c.CategoryName, (match item with null -> "" | _ -> item.ProductName)) }
|> teeGrid
let checkForLongCustomerNameLength =
query { for c in db.Customers do
exists (c.Address.Length > 10) }
|> teeGrid
let checkCustomerNameLengthsAreNotAllShort =
query { for c in db.Customers do
all (c.Address.Length < 10) }
|> teeGrid
let queryWithOrderByInStrangePosition =
query { for c in db.Customers do
sortBy c.City
where (c.Country = "UK")
select c.CompanyName }
|> teeGrid
let queryWithNestedQueryInLetBeforeFinalSelect =
query { for c in db.Customers do
let orders = query { for o in db.Orders do where (o.CustomerID = c.CustomerID); select o }
select (c.ContactName,orders) }
|> teeGrid
let queryWithExplicitNestedEnumerableQueryInLetBeforeFinalSelect =
query { for c in db.Customers do
let orders = query { for o in 0 .. 100 do select (o+1) }
select (c.ContactName,orders) }
|> teeGrid
let queryWithImplicitNestedEnumerableQueryInLetBeforeFinalSelect =
query { for c in db.Customers do
let orders = query { for o in 0 .. 100 do select (o+1) }
select (c.ContactName,orders) }
|> teeGrid
let queryWithNestedQueryInFinalSelect =
query { for c in db.Customers do
select (c.ContactName, query { for o in db.Orders do where (o.CustomerID = c.CustomerID); select o }) }
|> teeGrid
// The following example demonstrates how to use a composite key to join data from three tables:
let compositeKeyQuery =
query { for o in db.Orders do
for p in db.Products do
groupJoin d in db.Order_Details on ((o.OrderID, p.ProductID) = (d.OrderID, d.ProductID)) into details
for d in details do
select (o.OrderID, p.ProductID, d.UnitPrice) }
|> teeGrid
You can’t perform that action at this time.
