示例#1
0
        private static T Read <T>(LocalCrmDatabaseOrganizationService service, Guid id, ColumnSet cs, DelayedException exception) where T : Entity
        {
            var query = SchemaGetOrCreate <T>(service.Info).
                        Where("Id == @0", id);
            var entity = query.FirstOrDefault();

            if (entity == null)
            {
                entity              = Activator.CreateInstance <T>();
                entity.Id           = id;
                exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity);
                return(null);
            }

            if (!cs.AllColumns)
            {
                foreach (var key in entity.Attributes.Keys.Where(k => !cs.Columns.Contains(k)).ToList())
                {
                    entity.Attributes.Remove(key);
                }
            }

            service.RemoveFieldsCrmDoesNotReturn(entity);
            PopulateFormattedValues <T>(entity);
            return(entity);
        }
示例#2
0
        private static T ProcessEntityForReturn <T>(LocalCrmDatabaseOrganizationService service, ColumnSet cs, T entity, bool checkForAliasedValues) where T : Entity
        {
            if (!cs.AllColumns)
            {
                foreach (var key in entity.Attributes.Keys.Where(k => !cs.Columns.Contains(k) &&
                                                                 (!checkForAliasedValues || !(entity[k] is AliasedValue))).ToList())
                {
                    entity.Attributes.Remove(key);
                }
            }

            service.RemoveFieldsCrmDoesNotReturn(entity);
            PopulateFormattedValues(service.Info, entity);
            PopulateReferenceNames(service, entity);
            return(entity.Serialize().DeserializeEntity <T>());
        }
示例#3
0
        public static EntityCollection ReadEntities <T>(LocalCrmDatabaseOrganizationService service, QueryExpression qe) where T : Entity
        {
            _aliasedEntityCount = GetLinkedEntitiesWithoutAliasNameCount(qe);
            var query = SchemaGetOrCreate <T>(service.Info).AsQueryable();

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed - this updates the query expression
            HandleFilterExpressionsWithAliases(qe, qe.Criteria).ToList();

            query = qe.LinkEntities.Aggregate(query, (q, e) => CallJoin(service.Info, q, e));

            query = ApplyFilter(query, qe.Criteria);

            var entities = query.ToList();

            if (qe.Orders.Any())
            {
                // Sort
                var ordered = entities.Order(qe.Orders[0]);
                entities = qe.Orders.Skip(1).Aggregate(ordered, (current, t) => current.Order(t)).ToList();
            }

            if (!qe.ColumnSet.AllColumns)
            {
                foreach (var entity in entities)
                {
                    foreach (var key in entity.Attributes.Keys.Where(k => !qe.ColumnSet.Columns.Contains(k) && !(entity[k] is AliasedValue)).ToList())
                    {
                        entity.Attributes.Remove(key);
                    }
                }
            }

            foreach (var entity in entities)
            {
                service.RemoveFieldsCrmDoesNotReturn(entity);
                PopulateFormattedValues <T>(entity);
            }

            var result = new EntityCollection();

            result.Entities.AddRange(entities);
            return(result);
        }