• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTcwOQ==

Related Code
Get T-SQL for Linq to Sql expression [draft]

Left outer join extension method

Extension method to do a left outer join with linq expressions

0
554 0 0 0 0 0

For usage see Queryable.Join reference: http://msdn.microsoft.com/en-us/library/bb534644(v=vs.95).aspx.

It's uses a help class (JoinTuple<,>) for the groupjoin.

public static IQueryable<TResult> LeftOuterJoin<TOuter, TInner, TKey, TResult>(
    this IQueryable<TOuter> outer,
    IQueryable<TInner> inner,
    Expression<Func<TOuter, TKey>> outerKeySelector,
    Expression<Func<TInner, TKey>> innerKeySelector,
    Expression<Func<TOuter, TInner, TResult>> resultSelector)
{

    System.Type t1 = typeof(TOuter);
    System.Type t2 = typeof(IEnumerable<TInner>);
    System.Type groupJoinType = typeof(JoinTuple<TOuter, IEnumerable<TInner>>);
    var param1 = Expression.Parameter(t1, "w");
    var param2 = Expression.Parameter(t2, "f");
    var groupJoinMemberInitLambda = Expression.Lambda(
    	Expression.MemberInit(
    		Expression.New(groupJoinType),
    		Expression.Bind(groupJoinType.GetProperty("Item1"), param1),
    		Expression.Bind(groupJoinType.GetProperty("Item2"), param2)
    	), param1, param2);

    var q = outer.Provider.CreateQuery(
    	Expression.Call(
    		typeof(Queryable),
    		"GroupJoin",
    		new System.Type[]
    		{
    			typeof(TInner),
    			typeof(TOuter),
    			typeof(TKey),
    			groupJoinType
    		},
    		outer.Expression,
    		inner.Expression,
    		outerKeySelector,
    		innerKeySelector,
    		Expression.Quote(groupJoinMemberInitLambda)));

    ParameterExpression defaultIfParameter = Expression.Parameter(groupJoinType, "f");
    MethodCallExpression defaultIfEmptyExpression2 =
    	Expression.Call(
    		typeof(Enumerable),
    		"DefaultIfEmpty",
    		new System.Type[] { typeof(TInner) },
    		Expression.Property(defaultIfParameter, "Item2"));
    LambdaExpression defaultIfEmptyLambda2 = Expression.Lambda(defaultIfEmptyExpression2, defaultIfParameter);

    var p1 = Expression.Parameter(groupJoinType, "t");
    var p2 = Expression.Parameter(typeof(TOuter), "o");
    var resultInvokeExpression = Expression.Invoke(resultSelector, Expression.Property(p1, "Item1"), p2);

    return q.Provider.CreateQuery(
    	Expression.Call(
    		typeof(Queryable),
    		"SelectMany",
    		new System.Type[]
    		{
    			groupJoinType,
    			typeof(TInner),
    			typeof(TResult)
    		},
    		q.Expression,
    		defaultIfEmptyLambda2,
    		Expression.Lambda(
    			resultInvokeExpression,
    			p1,
    			p2))) as IQueryable<TResult>;
}


public class JoinTuple<T1, T2>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
}

Share: twitter | facebook   Action: used | fork | flag

linq

Mark 'linq' tag as 'like'

Mark 'linq' tag as 'ignore'

linq-expressions

Mark 'linq-expressions' tag as 'like'

Mark 'linq-expressions' tag as 'ignore'


 @toha73 "Code Contributor"
716
August 13, 2012 5:03 PM
edited August 13, 2012 5:10 PM

Fork

 Left outer join extension method -  @toha73 Monday 13, 2012 5:03 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010