Matt Casto's .NET Journal RSS 2.0
 Friday, February 29, 2008
I just got into a nerd fight with another developer on my team about which style of LINQ join syntax looks better.

Normally I wouldn't be one to get too worked up about syntax differences like this, but in this case my code was changed from one to the other for no reason other than "I don't like the way your wrote it."

Which do you prefer?



or


Friday, February 29, 2008 3:29:42 PM (Eastern Standard Time, UTC-05:00)  #    Comments [10] -
programming
Friday, February 29, 2008 7:08:28 PM (Eastern Standard Time, UTC-05:00)
I prefer the LINQ syntax to the lambda syntax in this case. For these joins, I don't think either one is very nice. Why can't I just say:

from c in DataContext.Claims
left outer join l in Levels on c.LevelId == l.LevelId
select c;
Friday, February 29, 2008 9:34:44 PM (Eastern Standard Time, UTC-05:00)
Since I'm the one who rewrote the code I have a biased opinion. I prefer the methodized look more, but I don't like the way the Join works (four different inputs, and generics can't infer what is going on until you type it all out). Instead I have put together a different syntax that looks like this:

listOne.JoinTo(listTwo)
.On(one => one.FirstId)
.EqualTo(two => two.FirstId)
.Alias((one, two) => new { First = one, Second = two})
.Where(cmp => cmp.First.Title == "Title");

It still gives me that methodized feel, but also allows the parameters to be input one at a time, making generic inference work properly, and I think it reads a little nicer. The code I wrote to implement the join I'll include below

-----

public static class Extensions
{
public interface IEqualTo<T, T2, TKey>
{
IEnumerable<TResult> Alias<TResult>(Func<T, T2, TResult> composite);
}

public interface IOn<T, T2, TKey>
{
IEqualTo<T, T2, TKey> EqualTo(Func<T2, TKey> toFunction);
}

public interface IJoin<T, T2>
{
IOn<T, T2, TKey> On<TKey>(Func<T, TKey> fromFunction);
}

private class Join<T, T2> : IJoin<T, T2>
{
public IEnumerable<T> FromList { get; set; }
public IEnumerable<T2> ToList { get; set; }

public IOn<T, T2, TKey> On<TKey>(Func<T, TKey> fromFunction)
{
return new On<T, T2, TKey> { Join = this, FromFunction = fromFunction };
}
}

private class On<T, T2, TKey> : IOn<T, T2, TKey>
{
public IJoin<T, T2> Join { get; set; }
public Func<T, TKey> FromFunction { get; set; }


public IEqualTo<T,T2, TKey> EqualTo(Func<T2, TKey> toFunction)
{
return new Is<T, T2, TKey> { Join = Join, FromFunction = FromFunction, ToFunction = toFunction };
}

}

private class Is<T, T2, TKey> : IEqualTo<T, T2, TKey>
{
public IJoin<T, T2> Join { get; set; }
public Func<T, TKey> FromFunction { get; set; }
public Func<T2, TKey> ToFunction { get; set; }

public IEnumerable<TResult> Alias<TResult>(Func<T, T2, TResult> composite)
{
var join = ((Join<T, T2>)Join);
return join.FromList.Join(join.ToList, FromFunction, ToFunction, composite);
}
}

public static IJoin<T, T2> JoinTo<T, T2>(this IEnumerable<T> joinFrom, IEnumerable<T2> joinTo)
{
return new Join<T, T2> { FromList = joinFrom, ToList = joinTo };
}

}
Kris Scott
Friday, February 29, 2008 10:15:35 PM (Eastern Standard Time, UTC-05:00)
I would have used nHibernate.
Friday, February 29, 2008 10:19:06 PM (Eastern Standard Time, UTC-05:00)
Should have used sprocs...
Saturday, March 01, 2008 2:05:58 AM (Eastern Standard Time, UTC-05:00)
In this particular case I actually like the LINQ syntax, but in general I prefer to use Lambdas when they result in more concise and expressive code. For me, it's all about making the code more soluble.

@Kris, dude... you have GOT to start a blog! That syntax is hott and would make for some great blog fodder! At least write up a quick post and I'll host it on my blog... you can be a "Guest Blogger" or something. :)

@Steve Horn, I hope your tongue was firmly planted in the side of your cheek when you typed that comment! :)
Saturday, March 01, 2008 7:24:40 AM (Eastern Standard Time, UTC-05:00)
+1 for the 2nd way. Much easier to read for me and I think for everyone who is more trained in sql than lambdas.
Saturday, March 01, 2008 10:48:14 AM (Eastern Standard Time, UTC-05:00)
I prefer the more SQL-like syntax of the second version. Some could (and have) say that it only makes sense because I know SQL syntax, and that a developer unfamiliar with SQL would naturally prefer the first version because it looks more like C# method calls. I would have to answer that with "Find me a C# developer completely unfamiliar with basic SQL syntax". You certainly won't find one in my company, or any other company I've ever worked for.

Ha, I have won the conflict!
Sunday, March 02, 2008 9:47:26 PM (Eastern Standard Time, UTC-05:00)
Monday, March 03, 2008 8:34:43 AM (Eastern Standard Time, UTC-05:00)
@Mel Grubb: That's because you haven't read between the lines!
Monday, April 21, 2008 11:05:52 PM (Eastern Standard Time, UTC-05:00)
I think the second is much better. More like SQL and just easier to read. Have you done any performance testing to see which runs better?
Comments are closed.
Central Ohio Day of .NET

About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Matt Casto
Sign In
All Content © 2008, Matt Casto
Theme based on DasBlog theme 'Business' created by Christoph De Baene (delarou)