SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!

Joins Using LINQ Query Expressions in .NET 3.5

(Aug 21 2007 - 07:50:33 PM by Timothy Khouri) - [print blog post]

The more I use Visual Studio 2008 (and in particular, the new language features of .NET 3.5), the more I wonder how I ever programmed without it. My current love is the "Language Integrated Query" (LINQ) features that have been introduced in .NET 3.5 (with LINQ to SQL, XML, Entities etc). What I'm talking about right now is just the language itself (as the below example would just be considered LINQ to Objects).

If you're used to using SQL, then you're probably familiar with the "joining" (linking multiple sources together in your "from clause"). You've probably thought before, "this functionality would be great in every day programming, not just in SQL". Here is a quick snippet of C# code that shows how to perform joining using LINQ. This is just straight query expressions here, nothing special.

var result = from article in this.Articles
           join author in this.Authors
           on article.Author equals author.Name
           select new
              {
                  ArticleTitle = article.Title,
                  ArticleBody = article.Body,
                  AuthorName = author.Name,
                  AuthorDateOfBirth = author.DateOfBirth
              };

Those two objects (this.Articles and this.Authors) are simply generic lists of a couple of objects I've created. If you'd like to see the whole thing to get more insight on how easy it is to join in LINQ, here's the code:

public class Article
{
  public string Title { get; set; }

  public string Body { get; set; }

  public string Author { get; set; }
}

private class Author
{
  public string Name { get; set; }

  public DateTime DateOfBirth { get; set; }
}

private List<Article> Articles = new List<Article>();

private List<Author> Authors = new List<Author>();
  • Aug 22 2007 - 05:02:45 PM ReggieMcKall

    What features of LINQ are you claiming were included in .NET 3.0? LINQ was first introduced in .NET 3.5, not further expressed.

  • Aug 22 2007 - 10:12:27 PM Timothy Khouri

    I have to apologize for saying ".NET 3.0" with LINQ. While that was original intent, it didn't get pushed out until 3.5 (as you mentioned). At first I was thinking you were crazy, but then I realized my mistake was that it's the C# 3.0 language spec, not .NET 3.0.

    I'm not going to try to justify my statements (as it was incorrect, so I wouldn't want to justify that), but it is a common mistake (Google search for 'LINQ ".net 3.0"' and you'll see enough MSDN blogs, articles, mvp blogs etc talking about their excitement about "LINQ in .NET 3.0").

    Thanks for the correction (I'll fix the blog entry)!

  • Aug 23 2007 - 09:59:52 AM Timothy Khouri

    Hey, thanks again for the correction... I've fixed the blog entry to be more accurate :)

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Jan 05 2009 - 06:55:07 PM - (0.0937476)