A lot of what is special about Visual Studio 2008 from a language perspective has to do with a new feature set called LINQ (for Language-Integrated Query). LINQ is a new programming model that takes advantage of many of the features. It provides language extensions that are meant to change the way you access and work with data. With it, you can work with your data using object syntax and query collections of objects using VB and C#.

You can use LINQ to map between data table and objects. In this way, you get an easier, more productive way to work with your data. This includes full IntelliSense support based on table and column names. It also includes support for managing inserts, updates, deletes, and reads.

The last of these, reading data, is a big part of LINQ in that it has built-in support for easily querying collections of data. Using LINQ features, you can query not only your data but any collection in .NET. There are, of course, new keywords and syntax for doing so. Query operators that ship with Visual Basic, for example, include Select, From, Where, Join, Order By, Group By, Skip, Take, Aggregate, Let, and Distinct. The C# language has a similar set of keywords. And, if these are not enough, you can extend the built-in query operators, replace them, or write your own.

You use these query operators to query against any .NET data that implements the IEnumerable or IQueryable interface. This may include a DataTable, mapped SQL Server objects, .NET collections including Generics, DataSets, and XML data.

Let’s look at an example. Suppose you had a collection of employee objects called employees and you wanted to access all the employees at a specific location. To do so, you might write the following function:

C#
public static List<Employee> FilterEmployeesByLocation(

List<Employee> employees, string location) {

//LINQ query to return collection of employees filtered by location
var emps = from Employee in employees
where Employee.Location.City == location
select Employee;

return emps.ToList();
}


VB
Public Function FilterEmployeesByLocation( _

ByVal employees As List(Of Employee), _
ByVal location As String) As List(Of Employee)

‘LINQ query to return collection of employees filtered by location

Dim emps = From Employee In employees _
Where Employee.Location.City = location

Return emps.ToList()
End Function

Take a look at what is going on in the previous listing. The function takes a list of employee objects, filters it by a region passed to it, and then returns the resulting list. Notice that to filter the list, we create a LINQ, in-memory query called emps. This query can be read like this: Looking at all the employee objects inside the employees collection, find those whose city matches the city passed into the function. Finally, the emps.ToList() method call in the return statement converts the in-memory query results into a new collection.

This is just a brief overview of LINQ. There are many things going on here, such as compile-time checking and schema validation—not to mention the new LINQ language syntax. You.

Source of Information : Sams Microsoft Visual Studio 2008 Unleashed

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner