Ejemplo n.º 1
0
        public static Expression <Func <T, bool> > BuildPrimaryKeyExpression <T, TKey>(this IMetaDataProvider repository, TKey id)
            where T : class
        {
            var primaryKey = repository.GetPrimaryKey <T>().First();
            var parameter  = Expression.Parameter(typeof(T));
            var left       = Expression.Convert(Expression.PropertyOrField(parameter, primaryKey), typeof(TKey));
            var body       = Expression.Equal(left, Expression.Constant(id, typeof(TKey)));
            var predicate  = Expression.Lambda <Func <T, bool> >(body, parameter);

            return((Expression <Func <T, bool> >)Evaluator.PartialEval(predicate));
        }
Ejemplo n.º 2
0
        public static Expression <Func <T, bool> > BuildPrimaryKeyExpression <T, ID>(this IMetaDataProvider repository, ID id)
            where T : class
        {
            var primaryKey = repository.GetPrimaryKey <T>().First();

            var parameter  = Expression.Parameter(typeof(T));
            var body       = Expression.Convert(Expression.PropertyOrField(parameter, primaryKey), typeof(ID));
            var idSelector = Expression.Lambda <Func <T, ID> >(body, parameter);
            var predicate  = idSelector.BuildOrExpression(new[] { id });

            return(predicate);
        }
Ejemplo n.º 3
0
        IEnumerable <T> IBulkOperationsProvider.GetById <T, TKey>(IEnumerable <TKey> ids)
        {
            if (typeof(OID).IsAssignableFrom(typeof(TKey)))
            {
                return(ids.Select(id => (T)_context.Session.GetObjectFromId((OID)id)));
            }

            var primaryKey = _metaDataProvider.GetPrimaryKey <T>().First();

            var parameter  = Expression.Parameter(typeof(T));
            var body       = Expression.Convert(Expression.PropertyOrField(parameter, primaryKey), typeof(TKey));
            var idSelector = Expression.Lambda <Func <T, TKey> >(body, parameter);

            var predicate = idSelector.BuildOrExpression(ids.ToList());

            return(All <T>().Where(predicate).AsEnumerable());
        }
Ejemplo n.º 4
0
        public IEnumerable <T> GetById <T, ID>(IList <ID> ids) where T : class
        {
            if (typeof(OID).IsAssignableFrom(typeof(T)))
            {
                return(ids.Select(id => (T)_context.Session.GetObjectFromId((OID)id)));
            }
            else
            {
                var primaryKey = _metaDataProvider.GetPrimaryKey <T>().First();

                var parameter  = Expression.Parameter(typeof(T));
                var body       = Expression.Convert(Expression.PropertyOrField(parameter, primaryKey), typeof(ID));
                var idSelector = Expression.Lambda <Func <T, ID> >(body, parameter);

                var predicate = idSelector.BuildOrExpression <T, ID>(ids);

                return(this.All <T>().Where(predicate).AsEnumerable());
            }
        }
Ejemplo n.º 5
0
        public static Expression <Func <T, bool> > BuildPrimaryKeyExpression <T>(this IMetaDataProvider repository, T entity)
            where T : class
        {
            var primaryKeyValue = repository.GetPrimaryKeyValue(entity);
            var primaryKey      = repository.GetPrimaryKey <T>();

            var values = primaryKey.Zip(primaryKeyValue, Tuple.Create).ToArray();

            Expression <Func <T, bool> > predicate = null;

            foreach (var value in values)
            {
                var parameter = Expression.Parameter(typeof(T));
                var left      = Expression.Convert(Expression.PropertyOrField(parameter, value.Item1), value.Item2.GetType());
                var body      = Expression.Equal(left, Expression.Constant(value.Item2));
                predicate = predicate == null?Expression.Lambda <Func <T, bool> >(body, parameter) : predicate.And(Expression.Lambda <Func <T, bool> >(body, parameter));
            }

            return((Expression <Func <T, bool> >)Evaluator.PartialEval(predicate));
        }