forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule1.vb
827 lines (693 loc) · 26.8 KB
/
Module1.vb
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Collections
Imports System.Collections.Generic
Module Module1
Sub Main()
' LINQ TO SQL Query Example Snippets
Dim db As New Northwnd("c:\northwnd.mdf")
'''''''''''''''''''''''''''' A G G R E G A T E S ''''''''''''''''''
''''''''''''''''''''''' A V E R A G E ''''''''''''''''''''''''''
' <Snippet1>
Dim averageFreight = Aggregate ord In db.Orders _
Into Average(ord.Freight)
Console.WriteLine(averageFreight)
' </Snippet1>
' <Snippet2>
Dim averageUnitPrice = Aggregate prod In db.Products _
Into Average(prod.UnitPrice)
Console.WriteLine(averageUnitPrice)
' </Snippet2>
' <Snippet3>
Dim priceQuery = From prod In db.Products() _
Group prod By prod.CategoryID Into grouping = Group _
Select CategoryID, _
ExpensiveProducts = _
(From prod2 In grouping _
Where prod2.UnitPrice > _
grouping.Average(Function(prod3) _
prod3.UnitPrice) _
Select prod2)
For Each grp In priceQuery
Console.WriteLine(grp.CategoryID)
For Each listing In grp.ExpensiveProducts
Console.WriteLine(listing.ProductName)
Next
Next
' </Snippet3>
''''''''''''''''''''''''''''''' C O U N T '''''''''''''''''''''''''''
' <Snippet4>
Dim customerCount = db.Customers.Count()
Console.WriteLine(customerCount)
' </Snippet4>
' <Snippet5>
Dim notDiscontinuedCount = Aggregate prod In db.Products _
Into Count(Not prod.Discontinued)
Console.WriteLine(notDiscontinuedCount)
' </Snippet5>
''''''''''''''''''''''''''''''''''' M A X '''''''''''''''''''''''''
' <Snippet6>
Dim latestHireDate = Aggregate emp In db.Employees _
Into Max(emp.HireDate)
Console.WriteLine(latestHireDate)
' </Snippet6>
' <Snippet7>
Dim maxUnitsInStock = Aggregate prod In db.Products _
Into Max(prod.UnitsInStock)
Console.WriteLine(maxUnitsInStock)
' </Snippet7>
' <Snippet8>
Dim maxQuery = From prod In db.Products() _
Group prod By prod.CategoryID Into grouping = Group _
Select CategoryID, _
MostExpensiveProducts = _
(From prod2 In grouping _
Where prod2.UnitPrice = _
grouping.Max(Function(prod3) prod3.UnitPrice))
For Each grp In maxQuery
Console.WriteLine(grp.CategoryID)
For Each listing In grp.MostExpensiveProducts
Console.WriteLine(listing.ProductName)
Next
Next
' </Snippet8>
'''''''''''''''''''''''''''' M I N ''''''''''''''''''''
' <Snippet9>
Dim lowestUnitPrice = Aggregate prod In db.Products _
Into Min(prod.UnitPrice)
Console.WriteLine(lowestUnitPrice)
' </Snippet9>
' <Snippet10>
Dim lowestFreight = Aggregate ord In db.Orders _
Into Min(ord.Freight)
Console.WriteLine(lowestFreight)
' </Snippet10>
' <Snippet11>
Dim minQuery = From prod In db.Products() _
Group prod By prod.CategoryID Into grouping = Group _
Select CategoryID, LeastExpensiveProducts = _
From prod2 In grouping _
Where prod2.UnitPrice = grouping.Min(Function(prod3) _
prod3.UnitPrice)
For Each grp In minQuery
Console.WriteLine(grp.CategoryID)
For Each listing In grp.LeastExpensiveProducts
Console.WriteLine(listing.ProductName)
Next
Next
' </Snippet11>
'''''''''''''''''''''''''''' S U M '''''''''''''''''''''''''''
' <Snippet12>
Dim totalFreight = Aggregate ord In db.Orders _
Into Sum(ord.Freight)
Console.WriteLine(totalFreight)
' </Snippet12>
' <Snippet13>
Dim totalUnitsOnOrder = Aggregate prod In db.Products _
Into Sum(prod.UnitsOnOrder)
Console.WriteLine(totalUnitsOnOrder)
' </Snippet13>
''''''''''''' E N D O F A G G R E G A T E S ''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''' F I R S T ''''''''''''''''''''''''
' <Snippet14>
Dim shipper As Shipper = db.Shippers.First()
Console.WriteLine("ID = {0}, Company = {1}", shipper.ShipperID, _
shipper.CompanyName)
' </Snippet14>
' <Snippet15>
Dim custquery As Customer = _
(From c In db.Customers _
Where c.CustomerID = "BONAP" _
Select c) _
.First()
Console.WriteLine("ID = {0}, Contact = {1}", custquery.CustomerID, _
custquery.ContactName)
' </Snippet15>
''''''''''''''''''''''' T A K E / S K I P ''''''''''''''''''
' <Snippet16>
Dim firstHiredQuery = _
From emp In db.Employees _
Select emp _
Order By emp.HireDate _
Take 5
For Each empObj As Employee In firstHiredQuery
Console.WriteLine("{0}, {1}", empObj.EmployeeID, _
empObj.HireDate)
Next
' </Snippet16>
' <Snippet17>
Dim lessExpensiveQuery = _
From prod In db.Products _
Select prod _
Order By prod.UnitPrice Descending _
Skip 10
For Each prodObj As Product In lessExpensiveQuery
Console.WriteLine(prodObj.ProductName)
Next
' </Snippet17>
' <Snippet18>
Dim custQuery2 = _
From cust In db.Customers _
Order By (cust.ContactName) _
Select cust _
Skip 50 _
Take 10
For Each custRecord As Customer In custQuery2
Console.WriteLine(custRecord.ContactName)
Next
' </Snippet18>
' <Snippet19>
Dim custQuery3 = _
From custs In db.Customers _
Where custs.City = "London" _
Select custs _
Order By custs.CustomerID _
Skip 1 _
Take 1
For Each custObj In custQuery3
Console.WriteLine(custObj.CustomerID)
Next
' </Snippet19>
'''''''''''''''''''''''''''''' S O R T ''''''''''''''''''''''''''
' <Snippet20>
Dim hireQuery = _
From emp In db.Employees _
Select emp _
Order By emp.HireDate
For Each empObj As Employee In hireQuery
Console.WriteLine("EmpID = {0}, Date Hired = {1}", _
empObj.EmployeeID, empObj.HireDate)
Next
' </Snippet20>
' <Snippet21>
Dim freightQuery = _
From ord In db.Orders _
Where ord.ShipCity = "London" _
Select ord _
Order By ord.Freight
For Each ordObj In freightQuery
Console.WriteLine("Order ID = {0}, Freight = {1}", _
ordObj.OrderID, ordObj.Freight)
Next
' </Snippet21>
' S22 lives in separate method.
' There is no S23.
' S24 lives in separate method.
' <Snippet25>
Dim ordQuery = _
From ord In db.Orders _
Where CInt(ord.EmployeeID.Value) = 1 _
Select ord _
Order By ord.ShipCountry, ord.Freight Descending
For Each ordObj In ordQuery
Console.WriteLine("Country = {0}, Freight = {1}", _
ordObj.ShipCountry, ordObj.Freight)
Next
' </Snippet25>
' <Snippet26>
Dim highPriceQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group _
Order By CategoryID _
Select CategoryID, _
MostExpensiveProducts = _
From prod2 In grouping _
Where prod2.UnitPrice = _
grouping.Max(Function(p3) p3.UnitPrice)
For Each prodObj In highPriceQuery
Console.WriteLine(prodObj.CategoryID)
For Each listing In prodObj.MostExpensiveProducts
Console.WriteLine(listing.ProductName)
Next
Next
' </Snippet26>
' <Snippet27>
Dim prodQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group
For Each grp In prodQuery
Console.WriteLine(vbNewLine & "CategoryID Key = {0}:", _
grp.CategoryID)
For Each listing In grp.grouping
Console.WriteLine(vbTab & listing.ProductName)
Next
Next
' </Snippet27>
' <Snippet28>
Dim query = From p In db.Products _
Group p By p.CategoryID Into g = Group _
Select CategoryID, MaxPrice = g.Max(Function(p) p.UnitPrice)
' </Snippet28>
' <Snippet29>
Dim q2 = From p In db.Products _
Group p By p.CategoryID Into g = Group _
Select CategoryID, AveragePrice = g.Average(Function(p) _
p.UnitPrice)
' </Snippet29>
' S30 lives in separate method.
' <Snippet31>
Dim disconQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group _
Select CategoryID, NumProducts = grouping.Count(Function(p) _
p.Discontinued)
For Each prodObj In disconQuery
Console.WriteLine("CategoryID = {0}, Discontinued# = {1}", _
prodObj.CategoryID, prodObj.NumProducts)
Next
' </Snippet31>
' <Snippet32>
Dim prodCountQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group _
Where grouping.Count >= 10 _
Select CategoryID, ProductCount = grouping.Count
For Each prodCount In prodCountQuery
Console.WriteLine("CategoryID = {0}, Product count = {1}", _
prodCount.CategoryID, prodCount.ProductCount)
Next
' </Snippet32>
' S 33, S34 live in separate methods.
' <Snippet35>
Dim custRegionQuery = From cust In db.Customers _
Group cust.ContactName By Key = New With _
{cust.City, cust.Region} Into grouping = Group
For Each grp In custRegionQuery
Console.WriteLine(vbNewLine & "Location Key: {0}", grp.Key)
For Each listing In grp.grouping
Console.WriteLine(vbTab & "{0}", listing)
Next
Next
' </Snippet35>
''''''''''''''''''''''''''' D I S T I N C T '''''''''''''''
' <Snippet36>
Dim cityQuery = _
(From cust In db.Customers _
Select cust.City).Distinct()
For Each cityString In cityQuery
Console.WriteLine(cityString)
Next
' </Snippet36>
''''''''''''''' A N Y O R A L L '''''''''''''''''''
' <Snippet37>
Dim OrdersQuery = _
From cust In db.Customers _
Where cust.Orders.Any() _
Select cust
' </Snippet37>
' S37v is VB only and lives in separate section below.
''''''''''''''''''' C O N C A T E N A T E '''''''''''''''''''''
' S39 lives in separate method.
' <Snippet40>
Dim infoQuery = _
(From cust In db.Customers _
Select Name = cust.CompanyName, Phone = cust.Phone) _
.Concat _
(From emp In db.Employees _
Select Name = emp.FirstName & " " & emp.LastName, _
Phone = emp.HomePhone)
For Each infoData In infoQuery
Console.WriteLine("Name = " & infoData.Name & _
", Phone = " & infoData.Phone)
Next
' </Snippet40>
''''''''''''''''''''''' S E T D I F F E R E N C E '''''''''''''''
' S41 lives in separate method
''''''''''''''''''' S E T I N T E R S E C T I O N ''''''''''''
' S42 lives in separate method.
''''''''''''''''''' S E T U N I O N ''''''''''''''''''''''
' S43 lives in separate method.
''''''''''' C O N V E R T S E Q T O A R R A Y '''''''''''
' S44 lives in separate method.
'''''' C O N V E R T S E Q T O G E N L I S T '''''''
' <Snippet45>
Dim empQuery = _
From emp In db.Employees _
Where emp.HireDate.Value >= #1/1/1994# _
Select emp
Dim qList As List(Of Employee) = empQuery.ToList()
' </Snippet45>
''''''' C O N V E R T T Y P E T O G E N I E N U M ''''''''
' S46 lives in separate section.
''''''''''''''''''''''''''''' J O I N S '''''''''''''''''''''''
' There are 11 of these (one is VB only). All live in separate methods.
' S47 thru S56 inclusive, plus S50v for VB only.
'''''''''''''' F O R M U L A T E P R O J E C T I O N S '''''''''
' There are 9 of these: S57 - S65 inclusive.
' <Snippet57>
Dim nameQuery = From cust In db.Customers _
Select cust.ContactName
' </Snippet57>
' S58 lives in separate method.
' <Snippet59>
Dim info2Query = From emp In db.Employees _
Select Name = emp.FirstName & " " & emp.LastName, _
Phone = emp.HomePhone
' </Snippet59>
' <Snippet60>
Dim specialQuery = From prod In db.Products _
Select prod.ProductID, HalfPrice = CDec(prod.UnitPrice) / 2
' </Snippet60>
' S61 and S62 live in separate methods.
' <Snippet63>
Dim contactQuery = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust.ContactName
' </Snippet63>
' S64 and S65 live in separate methods.
''''''''''''''''''' E N D O F Q U E R I E S ''''''''''''''''''''
End Sub
'''''''''''''''''''''''''' OUT OF SCOPE METHODS ''''''''''''''''''
Sub Method22()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet22>
Dim priceQuery = _
From prod In db.Products _
Select prod _
Order By prod.UnitPrice Descending
For Each prodObj In priceQuery
Console.WriteLine("Product ID = {0}, Unit Price = {1}", _
prodObj.ProductID, prodObj.UnitPrice)
Next
' </Snippet22>
End Sub
Sub Method24()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet24>
Dim custQuery = _
From cust In db.Customers _
Select cust _
Order By cust.City, cust.ContactName
For Each custObj In custQuery
Console.WriteLine("City = {0}, Name = {1}", custObj.City, _
custObj.ContactName)
Next
' </Snippet24>
End Sub
Sub Method30()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet30>
Dim priceQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group _
Select CategoryID, TotalPrice = grouping.Sum(Function(p) _
p.UnitPrice)
For Each grp In priceQuery
Console.WriteLine("Category = {0}, Total price = {1}", _
grp.CategoryID, grp.TotalPrice)
Next
' </Snippet30>
End Sub
Sub Method33()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet33>
Dim prodQuery = From prod In db.Products _
Group prod By Key = New With {prod.CategoryID, prod.SupplierID} _
Into grouping = Group
For Each grp In prodQuery
Console.WriteLine(vbNewLine & "CategoryID {0}, SupplierID {1}", _
grp.Key.CategoryID, grp.Key.SupplierID)
For Each listing In grp.grouping
Console.WriteLine(vbTab & listing.ProductName)
Next
Next
' </Snippet33>
End Sub
Sub Method34()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet34>
Dim priceQuery = From prod In db.Products _
Group prod By Key = New With {.Criterion = prod.UnitPrice > 10} _
Into grouping = Group Select Key, grouping
For Each prodObj In priceQuery
If prodObj.Key.Criterion = False Then
Console.WriteLine("Prices 10 or less:")
Else
Console.WriteLine("\nPrices greater than 10")
For Each listing In prodObj.grouping
Console.WriteLine("{0}, {1}", listing.ProductName, _
listing.UnitPrice)
Next
End If
Next
' </Snippet34>
End Sub
' Following code is vb only.
' <Snippet37v>
Public Sub ContactsAvailable()
Dim db As New Northwnd("c:\northwnd.mdf")
Dim result = _
(From cust In db.Customers _
Where Not cust.Orders.Any() _
Select cust).All(AddressOf ContactAvailable)
If result Then
Console.WriteLine _
("All of the customers who have made no orders have a contact name")
Else
Console.WriteLine _
("Some customers who have made no orders have no contact name")
End If
End Sub
Function ContactAvailable(ByVal contact As Object) As Boolean
Dim cust As Customer = CType(contact, Customer)
Return (cust.ContactTitle Is Nothing OrElse _
cust.ContactTitle.Trim().Length = 0)
End Function
' </Snippet37v>
Sub Method39()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet39>
Dim custQuery = _
(From c In db.Customers _
Select c.Phone) _
.Concat _
(From c In db.Customers _
Select c.Fax) _
.Concat _
(From e In db.Employees _
Select e.HomePhone)
For Each custData In custQuery
Console.WriteLine(custData)
Next
' </Snippet39>
End Sub
Sub Method41()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet41>
Dim infoQuery = _
(From cust In db.Customers _
Select cust.Country) _
.Except _
(From emp In db.Employees _
Select emp.Country)
' </Snippet41>
End Sub
Sub Method42()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet42>
Dim infoQuery = _
(From cust In db.Customers _
Select cust.Country) _
.Intersect _
(From emp In db.Employees _
Select emp.Country)
' </Snippet42>
End Sub
Sub Method43()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet43>
Dim infoQuery = _
(From cust In db.Customers _
Select cust.Country) _
.Union _
(From emp In db.Employees _
Select emp.Country)
' </Snippet43>
End Sub
Sub Method44()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet44>
Dim custQuery = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust
Dim qArray() As Customer = custQuery.ToArray()
' </Snippet44>
End Sub
Sub Method47()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet47>
Dim infoQuery = _
From cust In db.Customers, ord In cust.Orders _
Where cust.City = "London" _
Select ord
' </Snippet47>
End Sub
Sub Method48()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet48>
Dim infoQuery = _
From prod In db.Products _
Where prod.Supplier.Country = "USA" AndAlso _
CShort(prod.UnitsInStock) = 0 _
Select prod
' </Snippet48>
End Sub
Sub Method49()
Dim db As New Northwnd("c:\northwnd.mdf")
Dim infoQuery = _
From emp In db.Employees, empterr In emp.EmployeeTerritories _
Where emp.City = "Seattle" _
Select emp.FirstName, emp.LastName, _
empterr.Territory.TerritoryDescription
End Sub
Sub Method50()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet50>
Dim infoQuery = _
From e1 In db.Employees, e2 In e1.Employees _
Where e1.City = e2.City _
Select FirstName1 = e1.FirstName, _
LastName1 = e1.LastName, FirstName2 = e2.FirstName, _
LastName2 = e2.LastName, e1.City
' </Snippet50>
End Sub
Sub Method50v()
Dim db As New Northwnd("c:\northwnd.mdf")
' This snippet is for VB only,
' <Snippet50v>
Dim q1 = From c In db.Customers, o In db.Orders _
Where c.CustomerID = o.CustomerID _
Select c.CompanyName, o.ShipRegion
' Note that because the O/R designer generates class
' hierarchies for database relationships for you,
' the following code has the same effect as the above
' and is shorter:
Dim q2 = From c In db.Customers, o In c.Orders _
Select c.CompanyName, o.ShipRegion
For Each nextItem In q2
Console.WriteLine("{0} {1}", nextItem.CompanyName, _
nextItem.ShipRegion)
Next
' </Snippet50v>
End Sub
Sub Method51()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet51>
Dim q = From c In db.Customers _
Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
Into orders = Group _
Select c.ContactName, OrderCount = orders.Count()
' </Snippet51>
End Sub
Sub Method52()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet52>
Dim q = From c In db.Customers _
Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
Into ords = Group _
Group Join e In db.Employees On c.City Equals e.City _
Into emps = Group _
Select c.ContactName, ords = ords.Count(), emps = emps.Count()
' </Snippet52>
End Sub
Sub Method53()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet53>
Dim q = From e In db.Employees() _
Group Join o In db.Orders On e Equals o.Employee Into ords _
= Group _
From o In ords.DefaultIfEmpty() _
Select e.FirstName, e.LastName, Order = o
' </Snippet53>
End Sub
Sub Method54()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet54>
Dim q = From c In db.Customers _
Group Join o In db.Orders On c.CustomerID Equals o.CustomerID _
Into ords = Group _
Let z = c.City + c.Country _
From o In ords _
Select c.ContactName, o.OrderID, z
' </Snippet54>
End Sub
Sub Method55()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet55>
Dim q = From o In db.Orders _
From p In db.Products _
Group Join d In db.OrderDetails On New With {o.OrderID, _
p.ProductID} _
Equals New With {d.OrderID, d.ProductID} Into details _
= Group _
From d In details _
Select o.OrderID, p.ProductID, d.UnitPrice
' </Snippet55>
End Sub
Sub Method56()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet56>
Dim q = From o In db.Orders _
Group Join e In db.Employees On o.EmployeeID _
Equals e.EmployeeID Into emps = Group _
From e In emps _
Select o.OrderID, e.FirstName
' </Snippet56>
End Sub
Sub Method58()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet58>
Dim infoQuery = From cust In db.Customers _
Select cust.ContactName, cust.Phone
' </Snippet58>
End Sub
Sub Method61()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet61>
Dim prodQuery = From prod In db.Products _
Select prod.ProductName, Availability = _
If(prod.UnitsInStock - prod.UnitsOnOrder < 0, _
"Out Of Stock", "In Stock")
' </Snippet61>
End Sub
' <Snippet62>
Public Class Name
Public FirstName As String
Public LastName As String
End Class
Dim db As New Northwnd("c:\northwnd.mdf")
Dim empQuery = From emp In db.Employees _
Select New Name With {.FirstName = emp.FirstName, .LastName = _
emp.LastName}
' </Snippet62>
Sub Method64()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet64>
Dim custQuery = From cust In db.Customers _
Select cust.CustomerID, CompanyInfo = New With {cust.CompanyName, _
cust.City, cust.Country}, ContactInfo = _
New With {cust.ContactName, cust.ContactTitle}
' </Snippet64>
End Sub
Sub Method65()
Dim db As New Northwnd("c:\northwnd.mdf")
' <Snippet65>
Dim ordQuery = From ord In db.Orders _
Select ord.OrderID, DiscountedProducts = _
(From od In ord.OrderDetails _
Where od.Discount > 0.0 _
Select od), _
FreeShippingDiscount = ord.Freight
' </Snippet65>
End Sub
' <Snippet46>
Private Function isValidProduct(ByVal prod As Product) As Boolean
Return prod.ProductName.LastIndexOf("C") = 0
End Function
Sub ConvertToIEnumerable()
Dim db As New Northwnd("c:\northwnd.mdf")
Dim validProdQuery = _
From prod In db.Products.AsEnumerable _
Where isValidProduct(prod) _
Select prod
End Sub
' </Snippet46>
End Module