Ejemplo n.º 1
0
    public IQueryable <T> Entities <T>()
    {
        var provider = new MyQueryProvider(this);
        var query    = new MyQueryable <T>(provider, this);

        return(query);
    }
Ejemplo n.º 2
0
    private void CrearIQueryProperty(object obj, PropertyInfo p)
    {
        var provider = new MyQueryProvider(this);



        Join MyAttribute = (Join)Attribute.GetCustomAttributes(p, false).FirstOrDefault(t => t is Join);

        if (MyAttribute == null)
        {
            return;
        }

        PropertyInfo pJoin1 = obj.GetType().GetProperties().FirstOrDefault(t => t.GetCustomAttributes(typeof(Column), true).Length > 0 && ((Column)t.GetCustomAttributes(typeof(Column), true)[0]).Name == MyAttribute.Key);

        if (pJoin1 == null)
        {
            pJoin1 = obj.GetType().GetProperty(MyAttribute.Key);
        }
        object pJoinValue1 = pJoin1.GetValue(obj, null);

        Func <object, bool> g;
        var tipoBase          = p.PropertyType.GetGenericArguments()[0];
        var delegado          = typeof(Func <,>).MakeGenericType(tipoBase, typeof(bool));
        var delegadoExpresion = typeof(Expression <>).MakeGenericType(delegado);
        var param             = Expression.Parameter(tipoBase, "e");
        var predicate         = LambdaExpression.Lambda(delegado, Expression.Equal(Expression.Property(param, MyAttribute.ForeignKey),
                                                                                   Expression.Constant(pJoinValue1)),
                                                        new ParameterExpression[] { param });


        Type _tipo = typeof(MyQueryable <>).MakeGenericType(tipoBase);
        var  query = (IQueryable)Activator.CreateInstance(_tipo, provider, this);

        query = query.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Where",
                new Type[] { tipoBase },
                query.Expression, Expression.Quote(predicate)));

        p.SetValue(obj, query, null);
    }
        private static void UseMyQueryProvider()
        {
            var q1 = MyQueryProvider.CreateQueryable <int>();

            Console.WriteLine($"q1: {q1.Expression}");

            var q2 = q1.Where(x => x % 2 == 0);

            Console.WriteLine($"q2: {q2.Expression}");

            var q3 = q2.Select(x => x * x);

            Console.WriteLine($"q3: {q3.Expression}");

            // Executeメソッドがnullのみを返すのでこのforeachで"ランタイム"エラーとなる。
            // foreachのコメントアウトを外してもコンパイルは通る。
            //foreach (var val in q3)
            //{
            //   Console.WriteLine($"{val}");
            //}
        }
Ejemplo n.º 4
0
 public MyQuery(IQueryable <T> queryable)
 {
     _queryable = queryable;
     Provider   = new MyQueryProvider(_queryable.Provider);
 }