zoukankan      html  css  js  c++  java
  • A Join extension method for the dynamic Linq

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/4482b083-72bd-4415-82be-8cccbd9eabba

             

             public static IQueryable Join(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values)
            {
                
    if (inner == nullthrow new ArgumentNullException("inner");
                
    if (outerSelector == nullthrow new ArgumentNullException("outerSelector");
                
    if (innerSelector == nullthrow new ArgumentNullException("innerSelector");
                
    if (resultsSelector == nullthrow new ArgumentNullException("resultsSelctor");

                LambdaExpression outerSelectorLambda 
    = DynamicExpression.ParseLambda(outer.ElementType, null, outerSelector, values);
                LambdaExpression innerSelectorLambda 
    = DynamicExpression.ParseLambda(inner.AsQueryable().ElementType, null, innerSelector, values);

                ParameterExpression[] parameters 
    = new ParameterExpression[] {
                Expression.Parameter(outer.ElementType, 
    "outer"), Expression.Parameter(inner.AsQueryable().ElementType, "inner") };
                LambdaExpression resultsSelectorLambda 
    = DynamicExpression.ParseLambda(parameters, null, resultsSelector, values);

                
    return outer.Provider.CreateQuery(
                    Expression.Call(
                        
    typeof(Queryable), "Join",
                        
    new Type[] { outer.ElementType, inner.AsQueryable().ElementType, outerSelectorLambda.Body.Type, resultsSelectorLambda.Body.Type },
                        outer.Expression, inner.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(innerSelectorLambda), Expression.Quote(resultsSelectorLambda)));
            }


            
    //The generic overload.
            public static IQueryable<T> Join<T>(this IQueryable<T> outer, IEnumerable<T> inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values)
            {
                
    return (IQueryable<T>)Join((IQueryable)outer, (IEnumerable)inner, outerSelector, innerSelector, resultsSelector, values);
            }

     应用


    var aaa 
    = context.Dict.Where(p=>p.DictionaryId >3).Select(p => p.DictionaryId);

    var bbb = context.Dict.Join(aaa, "DictionaryId""it""outer"); 

    ----------------------------------- http://www.cnblogs.com/rock_chen/
  • 相关阅读:
    汉堡博客
    复利计算——结对1.0
    《构建之法》第4章读后感
    Compound Interest Calculator4.0
    实验一 命令解释程序的编写
    Compound Interest Calculator3.0续
    1203正规式转换为有穷自动机
    优缺点评价
    语文文法
    词法分析实验总结
  • 原文地址:https://www.cnblogs.com/rock_chen/p/1499589.html
Copyright © 2011-2022 走看看