Click here to Skip to main content
15,879,535 members
Articles / LINQ

Dynamically Evaluating a JOIN Expression with Linq

Rate me:
Please Sign up or sign in to vote.
4.71/5 (3 votes)
21 Feb 2010CPOL 18.7K   4   4
How to dynamically evaluate a JOIN expression with Linq

Introduction

For the last little while, I've been toying around with System.Linq.Expressions and a mini SQL parser to see how far I can go with evaluating plain text SQL expressions against arbitrary collections of objects. It's mostly a side project for my own enjoyment but eventually, I am hoping it actually turns into something broadly useful.

Results so far I've put on CodeProject:

It's been a while, but today I opened up that code again and got a basic JOIN operator working. This is cool because ultimately I'd like to be able to join loosely related datasources; say across the mp3 tags in my music collection and the data on my last.fm account.

So just now, I've been able to get this unit test to pass:

C#
[TestMethod]
public void SimpleJoin()
{
    IEnumerable source<Person> = TestData.GetPeople();
    IEnumerable families<Family> = TestData.GetFamilies();

    var answer = source.Join(families, p => p.Address, f => f.Address,
        (p, f) => new FamilyMember 
	{ Name = p.Name, LastName = f.Name, Location = f.Address });

    var result = source.Query<Person, Family, FamilyMember>
              ("SELECT Name, that.Name AS LastName, Address AS Location 
              FROM this INNER JOIN that ON this.Address = that.Address", families);

    Assert.IsTrue(result.SequenceEqual(answer));
}

This is cool because now I can start generating complex queries without having compile time knowledge of the underlying data structures. Once I get things fleshed out further, I'll update things on CodeProject.

This article was originally posted at http://spookycoding.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

 
QuestionWhere is the code source? Pin
elhumbertoz20-Sep-10 17:12
elhumbertoz20-Sep-10 17:12 
AnswerRe: Where is the code source? Pin
Don Kackman21-Sep-10 3:48
Don Kackman21-Sep-10 3:48 
GeneralCase Pin
elhumbertoz20-Sep-10 16:41
elhumbertoz20-Sep-10 16:41 
GeneralRe: Case Pin
Don Kackman21-Sep-10 3:50
Don Kackman21-Sep-10 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.