nVentive was present in Quebec city at HEROES happen {here}, Microsoft's launch for Visual Studio.Net 2008, SQL Server 2008 and Windows Server 2008.
François and I were experts there on the developer side of things, and the one thing we got asked the most, were questions on LINQ.
Everybody knows that LINQ is the hot new way to query objects in C# using a syntax similar to SQL. It depends on 2 tricks, Extension Methods and Lanbda Expressions.
This is a small sample.
public
class Blog{
public string Title { get; set; }}
class Program{
static void Main(string[] args) {
IList<Blog> blogs = new List<Blog>(); blogs.Add(
new Blog { Title = "3 cheese lasagna" }); blogs.Add(
new Blog { Title = "Blue cheese alfredo sauce" }); blogs.Add(
new Blog { Title = "Macaroni and cheese" }); blogs.Add(
new Blog { Title = "Angel cake" });
var blogsWithCheese = from blog in blogs
where blog.Title.ToLower().Contains("cheese")
select blog;
//blogsWithCheese contains 3 elements.
}
}
You can query something that is IEnumerable with LINQ to Objects, or a database itself using LINQ to SQL. If XML is your thing, there is a LINQ to XML variant and let's not forget that there is adapter to query Ado.Net DataSets. If you've heard of Entity Framework, LINQ will also work there to abstract it's object SQL language.
Now there's still more, if you have a source that you'd like to query with LINQ, it's just a matter of implementing IQueryProvider and IQueryable.
There are 2 tools I recommend you use in your adventures with LINQ:
- LINQPad is a interactive LINQ learning tool. Check it out, you'll never go back into Query Analyzer after that.
- Visual LINQ Query Builder is a plugin for Visual Studio 2008 that will help you develop your queries with a designer, now that's practical.
