/  69
 
101 LINQ to Objects Samples: From C# to APL+WinAjay Askoolum
 © Ajay AskoolumLanguage Integrated Query (LINQ) is an extension to C#, available from version 3.0
1
, that is, with VisualStudio 2008; LINQ is a standard for querying and updating data. The syntax of LINQ integrates with that of thehost language, C# or Visual Basic.Net. Here, I am interested in LINQ to Objects—the other implementationsare LINQ to SQL, LINQ to Entities, LINQ to DataSet, and LINQ to XML—that deals with in-memory data or,more generally, collections.APL+Win, A Programming Language (APL) has been about language integrated data manipulation—arrays, vectors, and nested or jagged arrays—throughout its history
2
. Here I am interested in LINQ to Objectsas a formal deliverable from APL+Win. With its strong/unique integration of Component Object Model (COM)technology into its language features, APL+Win delivers LINQ functionality either in conjunction with C# as aCOM Server or on a standalone basis.In this article, I present LINQ to Objects implemented in C# and APL+Win side by side. Above all, LINQprovides a platform for extolling the capabilities of APL+Win using the same vocabulary as a C# developer!The
small 
price of this opportunity is the acquisition of a grasp of the LINQ to Objects concepts.I urge you to read the ‘Partitioning Operators’ section even if LINQ to Objects is not within your sphere ofinterest right now. For APL+Win users, this group of operators will be very familiar. That C# is nowincorporating these features into the language does indeed suggest that APL+Win is a language that is aheadof time.
Table of Contents
Table of Contents........................................................................................................................1
 
1.1The Agenda......................................................................................................................5
 
1.1.1The terms of reference...............................................................................................................6
 
1.1.2The payoff for general APL+Win programming.........................................................................6
 
1.1.2.1Replicating the experience...................................................................................................................7
 
1.1.3APL+Win and Dot Net Accessibility...........................................................................................8
 
1.1.4Why LINQ to Objects?...............................................................................................................9
 
1.2Getting Started..................................................................................................................9
 
1.2.1Understanding the code.............................................................................................................9
 
1.2.1.1Language symbols.............................................................................................................................10
 
1.2.1.2What is the question?.........................................................................................................................11
 
1.3Restriction Operators......................................................................................................11
 
1.3.1Where - Simple 1.....................................................................................................................11
 
1.3.1.1C# code..............................................................................................................................................11
 
1.3.1.2APL+Win code..................................................................................................................................12
 
1.3.1.3Understanding the standard result.....................................................................................................12
 
1.3.1.4Method Signature..............................................................................................................................13
 
1.3.1.5Further tests.......................................................................................................................................14
 
1.3.2Where - Simple 2.....................................................................................................................14
 
1.3.3Where - Simple 3.....................................................................................................................16
 
1
Dot Net Framework 3.5.
2
Unfortunately, LINQ and APL+Win use different jargon to describe identical concepts; I expect that the LINQ vocabulary will prevail.
 
Page 2 of 691.3.4Where - Drilldown....................................................................................................................17
 
1.3.5Where - Indexed......................................................................................................................18
 
1.4Projection Operators.......................................................................................................19
 
1.4.1Select - Simple 1......................................................................................................................19
 
1.4.1.1Linq6 in focus....................................................................................................................................19
 
1.4.2Select - Simple 2......................................................................................................................20
 
1.4.3Select - Transformation............................................................................................................20
 
1.4.4Select - Anonymous Types 1...................................................................................................21
 
1.4.5Select - Anonymous Types 2...................................................................................................21
 
1.4.6Select - Anonymous Types 3...................................................................................................22
 
1.4.7Select - Indexed.......................................................................................................................22
 
1.4.8Select - Filtered........................................................................................................................23
 
1.4.9SelectMany - Compound from 1..............................................................................................23
 
1.4.10SelectMany - Compound from 2............................................................................................24
 
1.4.11SelectMany - Compound from 3............................................................................................24
 
1.4.12SelectMany - from Assignment..............................................................................................25
 
1.4.13SelectMany - Multiple from....................................................................................................26
 
1.4.14SelectMany - Indexed............................................................................................................27
 
1.5Partitioning Operators.....................................................................................................28
 
1.5.1Take - Simple...........................................................................................................................28
 
1.5.2Take - Nested..........................................................................................................................28
 
1.5.2.1First two customers in Washington and all their orders....................................................................29
 
1.5.2.2First 3 customers in Washington and their first 5 orders...................................................................29
 
1.5.3Skip - Simple............................................................................................................................29
 
1.5.4Skip - Nested...........................................................................................................................29
 
1.5.5TakeWhile - Simple..................................................................................................................30
 
1.5.6TakeWhile - Indexed................................................................................................................30
 
1.5.7SkipWhile - Simple...................................................................................................................31
 
1.5.8SkipWhile - Indexed.................................................................................................................31
 
1.6Ordering Operators.........................................................................................................31
 
1.6.1OrderBy - Simple 1..................................................................................................................31
 
1.6.2OrderBy - Simple 2..................................................................................................................32
 
1.6.3OrderBy - Simple 3..................................................................................................................32
 
1.6.3.1Where is the difference?....................................................................................................................32
 
1.6.4OrderBy - Comparer................................................................................................................33
 
1.6.5OrderByDescending - Simple 1...............................................................................................33
 
1.6.6OrderByDescending - Simple 2...............................................................................................33
 
1.6.7OrderByDescending - Comparer.............................................................................................34
 
1.6.8ThenBy - Simple......................................................................................................................34
 
1.6.9ThenBy - Comparer.................................................................................................................35
 
1.6.10ThenByDescending - Simple.................................................................................................35
 
1.6.11ThenByDescending - Comparer............................................................................................35
 
1.6.12Reverse..................................................................................................................................36
 
1.7Grouping Operators........................................................................................................36
 
1.7.1GroupBy - Simple 1..................................................................................................................36
 
1.7.2GroupBy - Simple 2..................................................................................................................37
 
1.7.3GroupBy - Simple 3..................................................................................................................37
 
1.7.4GroupBy - Nested....................................................................................................................38
 
1.7.5GroupBy – Comparer...............................................................................................................38
 
1.7.6GroupBy – Comparer, Mapped................................................................................................38
 
1.8Set Operators.................................................................................................................39
 
1.8.1Distinct - 1................................................................................................................................39
 
1.8.2Distinct - 2................................................................................................................................39
 
1.8.3Union - 1..................................................................................................................................40
 
1.8.4Union - 2..................................................................................................................................40
 
1.8.5Intersect - 1..............................................................................................................................41
 
1.8.6Intersect - 2..............................................................................................................................41
 
1.8.7Except - 1.................................................................................................................................42
 
1.8.8Except - 2.................................................................................................................................42
 
 
Page 3 of 69
1.9Conversion Operators.....................................................................................................43
 
1.9.1To Array...................................................................................................................................43
 
1.9.2To List......................................................................................................................................43
 
1.9.3To Dictionary............................................................................................................................43
 
1.9.4OfType.....................................................................................................................................44
 
1.10Element Operators........................................................................................................44
 
1.10.1First - Simple..........................................................................................................................44
 
1.10.2First – Condition (missing at URL).........................................................................................45
 
1.10.3First - Indexed........................................................................................................................45
 
1.10.4FirstOrDefault - Simple..........................................................................................................45
 
1.10.5FirstOrDefault - Condition......................................................................................................46
 
1.10.6FirstOrDefault - Indexed.........................................................................................................46
 
1.10.7ElementAt..............................................................................................................................47
 
1.11Generation Operators...................................................................................................48
 
1.11.1Range....................................................................................................................................48
 
1.11.2Repeat...................................................................................................................................48
 
1.12Quantifiers....................................................................................................................49
 
1.12.1Any - Simple...........................................................................................................................49
 
1.12.2Any - Indexed.........................................................................................................................49
 
1.12.3Any - Grouped........................................................................................................................49
 
1.12.4All - Simple.............................................................................................................................49
 
1.12.5All - Indexed...........................................................................................................................50
 
1.12.6All - Grouped..........................................................................................................................50
 
1.13Aggregate Operators....................................................................................................50
 
1.13.1Count - Simple.......................................................................................................................50
 
1.13.2Count - Conditional................................................................................................................51
 
1.13.3Count - Indexed.....................................................................................................................51
 
1.13.4Count - Nested.......................................................................................................................52
 
1.13.5Count - Grouped....................................................................................................................52
 
1.13.6Sum - Simple.........................................................................................................................52
 
1.13.7Sum - Projection....................................................................................................................53
 
1.13.8Sum - Grouped......................................................................................................................53
 
1.13.9Min - Simple...........................................................................................................................53
 
1.13.10Min - Projection....................................................................................................................54
 
1.13.11Min - Grouped......................................................................................................................54
 
1.13.12Min - Elements.....................................................................................................................54
 
1.13.13Max - Simple........................................................................................................................55
 
1.13.14Max - Projection...................................................................................................................55
 
1.13.15Max - Grouped.....................................................................................................................55
 
1.13.16Max - Elements....................................................................................................................56
 
1.13.17Average - Simple.................................................................................................................56
 
1.13.18Average - Projection............................................................................................................56
 
1.13.19Average - Grouped..............................................................................................................57
 
1.13.20Fold - Simple........................................................................................................................57
 
1.13.21Fold - Seed..........................................................................................................................57
 
1.14Miscellaneous Operators..............................................................................................58
 
1.14.1Concat - 1..............................................................................................................................58
 
1.14.2Concat - 2..............................................................................................................................58
 
1.14.3EqualAll - 1.............................................................................................................................58
 
1.14.4EqualAll - 2.............................................................................................................................59
 
1.14.4.1EqualAll - 2: Extension..................................................................................................................59
 
1.15Custom Sequence Operators.......................................................................................59
 
1.15.1Combine.................................................................................................................................60
 
1.15.1.1C#:APL+Win comparison...............................................................................................................60
 
1.16Query Execution...........................................................................................................61
 
1.16.1Deferred.................................................................................................................................61
 
1.16.2Immediate..............................................................................................................................61
 
1.16.3Query Reuse..........................................................................................................................62
 

Sections

show all« prev | next »

Share & Embed

More from this user

Recent Readcasters

Add a Comment

Characters: ...

uploaded a new revision for this document (#2)

12 / 27 / 2009