예제 #1
0
        public static Opcode GetCode(Expression expression, Opcode code, Type type)
        {
            var visitor = new LinqToSitecoreVisitor(code, type);

            visitor.EvalOpcodeExpression(expression);

            if (visitor.CodeTree == null)
            {
                var templateCode = new EqualsOperator(new FieldElement("@templatename"), new Literal(type.Name));

                var root = new Root {
                    NextStep = new Descendant(new ItemElement("*", new Predicate(templateCode)))
                };
                return(root);
            }
            return(visitor.CodeTree);
        }
        internal object Query(Expression expression)
        {
            var opcode  = LinqToSitecoreVisitor.GetCode(BooleanFixVisitor.Process <T>(expression), null, typeof(T));
            var context = new QueryContext(_database.DataManager);

            var qTranslator = LinqToSitecoreExtensions.GetQueryTranslator(context, _database);

            var contextResult = qTranslator.QueryFast(_database, opcode);

            var data = contextResult;

            if (data is QueryContext[])
            {
                return(((QueryContext[])data).Select(x => x.GetQueryContextItem().ReflectTo <T>(true)).ToList());
            }

            return(new List <T> {
                ((QueryContext)data).GetQueryContextItem().ReflectTo <T>(true)
            });
        }
        internal static List <T> Query <T>(Database database, Expression <Func <T, bool> > query, bool lazyLoading, params string[] include)
        {
            var db = database ?? Context.Database;

            var opcode  = LinqToSitecoreVisitor.GetCode(query, null, typeof(T));
            var context = new QueryContext(db.DataManager);

            var qTranslator = GetQueryTranslator(context, db);

            var contextResult = qTranslator.QueryFast(db, opcode);

            var data = contextResult;

            if (data is QueryContext[])
            {
                return(((QueryContext[])data).Select(x => x.GetQueryContextItem().ReflectTo <T>(lazyLoading, include))
                       .Where(query.Compile()).ToList());
            }

            return(new List <T> {
                ((QueryContext)data).GetQueryContextItem().ReflectTo <T>(lazyLoading, include)
            });
        }