Click here to Skip to main content
Click here to Skip to main content

Dynamically evaluating a JOIN expression with Linq

By , 21 Feb 2010
 

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:

[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.

License

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

About the Author

Don Kackman
Team Leader Starkey Laboratories
United States United States
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhere is the code source?memberelhumbertoz20 Sep '10 - 17:12 
Hello!
 
Where is the code for this project?
 
Thanks
hum

AnswerRe: Where is the code source?memberDon Kackman21 Sep '10 - 3:48 
I haven't yet posted updated code that includes JOINs. I have recently (just this week) gotten all of my unit tests to pass and will try to post an updated set of code sometime in the next week or two. It is significantly improved (I hope) from the previous versions; which were exploratory efforts.
 
I'll drop you a note when I get it posted.
10 PRINT "Software is hard. - D. Knuth"
20 GOTO 10

GeneralCasememberelhumbertoz20 Sep '10 - 16:41 
Please, helme with the CASE in SELECT:
 
Dim squery As String = "SELECT us.cd_menu, m.menutext, m.helptext, m.program, m.parameters, m.parent, " & vbLf &
            "   (CASE WHEN parent = 0 THEN 0 ELSE 1 END) AS HaveEvent, 0 AS IsChecked" & vbLf &
            "FROM Us_menu us, Menu m " & vbLf &
            "WHERE m.cd_menu=us.cd_menu AND cd_usuario= " + userId.ToString + " ORDER BY parent"
SIContext.ReleaseBuffers()
_Menu = SIContext.Menu.Query(squery)
 
I don't know why throw a exception in CASE WHEN parent = 0 THEN 0 ELSE 1 END. Please HELLPP MEEE.
 
Thanks
Humberto

GeneralRe: CasememberDon Kackman21 Sep '10 - 3:50 
The SQL syntax supported here is a derivative of SQL-89. It does not currently support the CASE statement.
10 PRINT "Software is hard. - D. Knuth"
20 GOTO 10

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Feb 2010
Article Copyright 2010 by Don Kackman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid