Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 4 comments about this article.
Article source: CoDe (2007 - Vol. 4 - Issue 1 - Sedna: Beyond Visual FoxPro 9 )


Article Pages:  1  2 - Next >


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.



Article Pages:  1  2 - Next Page: 'Other Data Sources' >>

Page 1: The Missing LINQ
Page 2: Other Data Sources

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
2.8 out of 5

40 people have rated this article.

TOWER 48

      LearnNow

 

LearnNow