Rock.Model.AttributeValueService.GetByAttributeIdAndEntityId C# (CSharp) Method

GetByAttributeIdAndEntityId() public method

Gets an Attribute Value by Attribute Id And Entity Id
public GetByAttributeIdAndEntityId ( int attributeId, int entityId ) : AttributeValue
attributeId int Attribute Id.
entityId int Entity Id.
return AttributeValue
        public AttributeValue GetByAttributeIdAndEntityId( int attributeId, int? entityId )
        {
            return Queryable()
                .Where( t =>
                    t.AttributeId == attributeId &&
                    (
                        ( !t.EntityId.HasValue && !entityId.HasValue ) ||
                        ( t.EntityId.HasValue && entityId.HasValue && t.EntityId.Value == entityId.Value )
                    )
                )
                .FirstOrDefault();
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Returns Global Attributes from cache.  If they are not already in cache, they
        /// will be read and added to cache
        /// </summary>
        /// <returns></returns>
        public static GlobalAttributesCache Read()
        {
            string cacheKey = GlobalAttributesCache.CacheKey();

            ObjectCache           cache            = MemoryCache.Default;
            GlobalAttributesCache globalAttributes = cache[cacheKey] as GlobalAttributesCache;

            if (globalAttributes != null)
            {
                return(globalAttributes);
            }
            else
            {
                globalAttributes                 = new GlobalAttributesCache();
                globalAttributes.Attributes      = new List <AttributeCache>();
                globalAttributes.AttributeValues = new Dictionary <string, KeyValuePair <string, string> >();

                var attributeService      = new Rock.Model.AttributeService();
                var attributeValueService = new Rock.Model.AttributeValueService();

                foreach (Rock.Model.Attribute attribute in attributeService.GetGlobalAttributes())
                {
                    var attributeCache = AttributeCache.Read(attribute);
                    globalAttributes.Attributes.Add(attributeCache);

                    var    attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, null).FirstOrDefault();
                    string value          = (attributeValue != null && !string.IsNullOrEmpty(attributeValue.Value)) ? attributeValue.Value : attributeCache.DefaultValue;
                    globalAttributes.AttributeValues.Add(attributeCache.Key, new KeyValuePair <string, string>(attributeCache.Name, value));
                }

                cache.Set(cacheKey, globalAttributes, new CacheItemPolicy());

                return(globalAttributes);
            }
        }
All Usage Examples Of Rock.Model.AttributeValueService::GetByAttributeIdAndEntityId