The Missing LINQ Visual FoxPro’s (VFP) Data Manipulation Language (DML) is one of VFP’s most compelling features. It is also the most obvious feature VFP developers miss in .NET languages such as C# and Visual Basic. However, Language Integrated Query (LINQ), a new query language for .NET developers, is a new feature in the upcoming releases of C# 3.0 and Visual Basic 9.0 that addresses these shortcomings. LINQ’s core features will seem very familiar to Visual FoxPro developers. LINQ provides the ability to execute SELECT statements as part of the core .NET languages, C# and Visual Basic. Anyone familiar with Visual FoxPro’s query commands or T-SQL’s SELECT syntax will find familiar commands and capabilities. However, LINQ does not aim to reproduce VFP/SQL Server features exactly. Instead, LINQ provides many unique features that go much beyond simple data query capabilities. Therefore, knowing other query languages is an advantage for developers who want to take advantage of LINQ, but at the same time, I recommend not getting too hung up on whether certain things are exactly identical to standardized SELECT-syntax. LINQ is a separate language with different features and somewhat different syntax. A Feature Overview So what exactly does LINQ do? Let me put it this way: The very first time I got a private introduction to LINQ quite some time ago, Anders Hejlsberg (the “father of C#”) told me the goal was to create query abilities inside of C# and Visual Basic that could “query anything that has structure.” So what is it that “has structure”? Well, in C# and Visual Basic, quite a lot as it turns out. First and foremost of course: data. This means that you can use LINQ to query data sources such as ADO.NET DataSets or SQL Server tables and views. But LINQ can query a lot more. XML also “has structure”. LINQ allows queries against any XML data source including an XML file or an XML string in memory. Objects also have structure. And of course, everything in .NET is an object. In fact, it turns out that LINQ is an engine that mainly queries objects, and features used to query “other” things, such as data or XML, are sitting on top of the object query engine. Let’s take a look at an example-an array of strings. Since both arrays and strings are objects in .NET, you can use LINQ to query from string arrays. Consider the following Visual Basic array of names: Dim names As String() names = New String(4) names(0) = "Smith" names(1) = "Snyder" names(2) = "Baker" names(3) = "Jonson" names(4) = "Ballmer"
Or the C# equivalent: string[] names; names = new string[5]; names[0] = "Smith"; names[1] = "Snyder"; names[2] = "Baker"; names[3] = "Jonson"; names[4] = "Ballmer";
Using LINQ you can query from these arrays. First I’ll show you an equivalent of SQL Server’s SELECT *. In Visual Basic, you’ll use this LINQ syntax to return all “fields” and all “records” from this array: From name In names Select name
Or in C#: from name in names select name;
As you can see, this is not exactly like a SELECT statement you know from VFP and SQL Server, but still similar. In T-SQL you would use this equivalent: SELECT name FROM names
You can see two main differences between these simple LINQ selects and the simple T-SQL SELECT. First, the LINQ statement seems to be backward. While T-SQL specifies first what to select and then where to select it from, LINQ goes the opposite way by specifying the source (the “from” part) first. In the world of strong typing and IntelliSense, the LINQ approach makes more sense. From a functional point of view however, the result remains the same. Second, T-SQL simply says “from names” while LINQ uses the seemingly more complex “from name in names” syntax. LINQ supports more possible sources than T-SQL. In T-SQL, “names” must be a table (or some equivalent source such as a view). In LINQ, the source could be any object containing other objects of any complexity. The above LINQ example specifies that within the “names” array, I expect items that I choose to each refer to “name”, allowing me to then use that “name” in various ways. In this very simple example LINQ queries the entire “name” into the result list, but in more complex examples (see below), LINQ can use the “name” item in different ways. The LINQ examples I’ve shown you so far are not very exciting since the resulting list is exactly the same as the source array. However, I’ll now spice things up a little bit. Consider this Visual Basic example: From name In names _ Order By name _ Where name.StartsWith("S") _ Select name
Or, once again, the C# equivalent: from name in names orderby name where name.StartsWith("S") select name;
These queries return only the names that start with “S” and sorts the result set. You can see how to use each item (referred to as “name” in this case) as part of the overall syntax. Without the “name in names” syntax, you couldn’t use “name.StartsWith()”. Now suppose I choose an array of complex objects instead of a simple string array, such as an array of customer objects, where each object has first and last name properties (among others). I might form a query like so: From customer In customers _ Order By customer.FirstName _ Where customer.LastName.StartsWith("S") _ Select New { customer.FullName, _ customer.Address}
In addition to the fact that this example uses properties on the “name” items, the actual “select” part of the statement is somewhat unusual. Instead of returning a list of customer objects, this example returns a list of new objects where each object in the list has “FullName” and “Address” properties. Note: Since each LINQ feature that I’ll discuss works equally well in Visual Basic and C# and the features have similar syntax, I will stop listing separate language examples. Now I’ll improve this example further by messing with the return value. Keep in mind that LINQ can return any object, allowing for much greater flexibility than you would typically expect from query statements. Consider this example: From customer In customers _ Order By customer.FirstName _ Where customer.LastName.StartsWith("S") _ Select New CustomerEditForm(customer.Key)
In this example, the result is a list of customer edit forms, each of which is instantiated with the primary key of the customer from the source list. This example shows a very interesting ability of LINQ queries: The result set can consist of things that weren’t even in the source. This is possible since LINQ has all the capabilities of .NET languages at its disposal. | & | | 
By: Markus Egger Markus is the founder and publisher of CODE Magazine and EPS' President and Chief Software Architect. He is also a Microsoft RD (Regional Director) and the one of the longest (if not THE longest) running Microsoft MVPs (Most Valuable Professionals). Markus is also a renowned speaker and author.
Markus' spends most of his time writing production code. The projects Markus has worked on include efforts for some of the world's largest companies including many Fortune 500 companies. Markus has also worked as a contractor for Microsoft (including the Visual Studio team). Markus has presented at many industry events, ranging from local user groups to major events such as MS TechEd. Markus' written work has been published extensively and in magazine ranging from MSDN Magazine, to Visual Studio Magazine, and of course in Markus' own CODE Magazine and much more. Markus is a supporter of communities in North America, Europe, and sometimes even beyond.
Markus currently focuses on development in .NET (Windows, Web, Windows Phone, and WinRT) as well as Android and iOS. He is passionate about overall application architecture, SOA, user interfaces and general development productivity and building maintainable and reusable systems.
In his spare time, Markus is an avid windsurfer, scuba diver, ice hockey player and world traveler. On a rainy day, he is known to enjoy a good game on his PC or Xbox.
megger@eps-software.com | Fast Facts | | LINQ provides to C# and Visual Basic what many Visual FoxPro developers have long known as a must-have feature: An integrated query language. However, LINQ goes beyond the ability to query data and instead queries data as well as XML and practically any sort of object data source. | |
|