Beispiel #1
0
        //public static EntityQuery BuildQuery(IEntity entity, NavigationProperty np) {
        //  var ekQuery = BuildQuery(entity.EntityAspect.EntityKey);
        //  var q = ekQuery.ExpandNonGeneric(np.Name);
        //  return q;
        //}

        /// <summary>
        /// Builds an <see cref="EntityQuery"/> to load the entities for the navigation property of the entity.
        /// </summary>
        public static EntityQuery BuildQuery(IEntity entity, NavigationProperty np)
        {
            if (np.IsScalar)
            {
                if (np.ForeignKeyNames.Count == 0)
                {
                    return(null);
                }
                var relatedKeyValues = np.ForeignKeyNames.Select(fk => entity.EntityAspect.GetValue(fk)).ToArray();
                var entityKey        = new EntityKey(np.EntityType, relatedKeyValues);
                return(BuildQuery(entityKey));
            }
            else
            {
                var inverseNp = np.Inverse;
                var fkNames   = inverseNp != null ? inverseNp.ForeignKeyNames : np.InvForeignKeyNames;
                if (fkNames.Count == 0)
                {
                    return(null);
                }
                var keyValues = entity.EntityAspect.EntityKey.Values;

                var parameterExpr   = Expression.Parameter(np.EntityType.ClrType, "t");
                var propExpressions = fkNames.Select(name => Expression.Property(parameterExpr, name));
                var fkExpression    = BuildMultiEqualExpr(propExpressions, keyValues);

                var entityQuery = EntityQuery.Create(np.EntityType.ClrType);
                var queryLambda = Expression.Lambda(fkExpression, parameterExpr);
                return(AddWhereClause(entityQuery, queryLambda));
            }
        }
        /// <summary>
        /// Returns a query that will return an Empty enumeration of specified type when executed.
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public static EntityQuery BuildEmptyQuery(Type entityType)
        {
            var parameterExpr = Expression.Parameter(entityType, "t");
            // where 1 = 0;
            var predicateExpr = Expression.Equal(Expression.Constant(1), Expression.Constant(0));
            var lambdaExpr    = Expression.Lambda(predicateExpr, parameterExpr);
            var entityQuery   = EntityQuery.Create(entityType);

            entityQuery = AddWhereClause(entityQuery, lambdaExpr);
            return(entityQuery);
        }
        /// <summary>
        /// Builds an <see cref="EntityQuery"/> based on a collection of <see cref="EntityKey"/>s.
        /// The EntityQuery returned is actually an EntityQuery{T} but T is unknown at compile time.
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static EntityQuery BuildQuery(IEnumerable <EntityKey> keys)
        {
            if (!keys.AllEqual(k => k.EntityType))
            {
                throw new ArgumentException("The EntityQueryBuilder.BuildQuery method requires that the 'keys' parameter consist of EntityKeys all with the same EntityType.");
            }
            var firstKey = keys.FirstOrDefault();

            if (firstKey == null)
            {
                return(null);
            }
            var entityQuery = EntityQuery.Create(firstKey.EntityType.ClrType);

            return(AddWhereClause(entityQuery, keys));
        }