Microsoft is about to release Visual Studio 2008 and .NET Framework 3.5; one of its greatest features is LINQ (which stands for Language-Integrated Query). This feature allows programmers to use query statements (select, where, join, etc) on .NET programming languages, such as C# and Visual Basic.
I have been trying Visual Web Developer 2008 Express Beta 2 (which you can download from here), and it seems good. As it's still beta, it's normal to see some things which still need a fix, particularly, I had a small problem with the properties windows, but nothing serious; just re-started the program and it worked fine. I haven't checked all the features yet. I’ve been playing around with LINQ and it seems really amazing, so I decided to write about it.
The basics of LINQ is to query arrays or collections of objects the same way we do to query a table in a database. Ok, it's obviously much more than that, but it's just a single post, not a fancy article. What I want to show here is how simple it is to use LINQ.
The code below shows how to perform a simple queriy with a custom object collections. Note that I have created a custom class named People which is just used to hold data, and another class named LinqSample1, which actually make the things happen. I have created an object named pplIKnow, which is a generic collection of People objects and I have populated it on the constructor of LinqSample1.
using System;
using System.Linq;
using System.Collections.Generic;
public class People {
public People() { }
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsFriend { get; set; }
}
public class LinqSample1 {
private List<People> pplIKnow = new List<People>();
pplIKnow.Add(new People {
Id = 1, IsFriend = true,
FirstName = "Peter", LastName = "Parker"
});
pplIKnow.Add(new People {
Id = 2, IsFriend = false,
FirstName = "Tony", LastName = "Stark"
});
pplIKnow.Add(new People {
Id = 3, IsFriend = false,
FirstName = "Maria", LastName = "Hill"
});
pplIKnow.Add(new People {
Id = 4, IsFriend = true,
FirstName = "Hiro", LastName = "Nakamura"
});
pplIKnow.Add(new People {
Id = 5, IsFriend = true,
FirstName = "Jack", LastName = "Bauer"
});
}
public IEnumerable<People> ListFriends() {
var _result = from p in pplIKnow
where p.IsFriend
orderby p.LastName ascending
select p;
return _result;
}
public IEnumerable<People> ListEnemies() {
var _result = from p in pplIKnow
where !p.IsFriend
orderby p.LastName ascending
select p;
return _result;
}
}
I have added another two methods to my class, ListFriends and ListEnemies. And as you can see, it's really, really simple to perform the query. It's not the same as SQL queries once you have to filter, join and what ever prior select the columns you want, but still, it's that simple. I guess Mr. Anders Heijsberg has done his homework pretty well when he came up with LINQ.
Once you have a class or a class library which performs the queries, you can use their methods on the UI the same way we do with ASP.NET 2.0. Just as an example, I have created a webpage on my project and added two grid views to display the results of the methods I have on the class LinqSample1. Then I have added two ObjectDataSource objects, so this is how my web form looks like:
On the ObjectDataSources I have chosen class LinqSample1 as my business object and the corresponding methods to each GridView. Simple as that. On the next post about LINQ, I intend to go deeper on this subject because I really liked LINQ and I believe it's gonna be really helpful for everyone who programs in .NET. And please note that .NET Framework or Visual Studio 2008 haven't been officially released. They are still beta.
LINQ is a strong part of .NET Framework 3.5; it's really simple to use, even John create a program with it, and just to finish, the picture about is the application running.
No comments:
Post a Comment