示例#1
0
 public static RelatedEntityCollection ToRelatedEntityCollection(this SortDetails details, string id)
 {
     if (details == null)
     {
         throw new ArgumentNullException("details", string.Format(Constants.ObjectNullException, "details"));
     }
     if (string.IsNullOrWhiteSpace(id))
     {
         throw new ArgumentNullException("id", string.Format(Constants.StringNullException, "id"));
     }
     return(new RelatedEntityCollection
     {
         Entity = details.EntityName,
         EntityId = id,
         RelatedEntity = details.RelatedEntity,
     });
 }
示例#2
0
        public List <RelatedEntityCollection> Sort(IEnumerable <T> entities, IEnumerable <RelatedEntity> relatedEntities, SortDetails details)
        {
            if (entities == null || !entities.Any() || relatedEntities == null || !relatedEntities.Any())
            {
                return(null);
            }
            var list                   = new List <RelatedEntityCollection>();
            var type                   = entities.First()?.GetType();
            var propInfoId             = type.GetProperty(details.EntityIdProperty);
            var entityPropertyPropInfo = type.GetProperty(details.EntityProperty);

            Type        dictIdType         = typeof(Dictionary <,>).MakeGenericType(propInfoId.PropertyType, typeof(RelatedEntityCollection));
            Type        dictPropType       = typeof(Dictionary <,>).MakeGenericType(entityPropertyPropInfo.PropertyType, dictIdType);
            Type        dictType           = typeof(Dictionary <,>).MakeGenericType(propInfoId.PropertyType, typeof(RelatedEntityCollection));
            IDictionary dict               = Activator.CreateInstance(dictPropType) as IDictionary;
            var         tryGetValueMethod  = dictPropType.GetMethod("TryGetValue");
            var         tryGetValueMethod2 = dictIdType.GetMethod("TryGetValue");

            foreach (var entity in entities)
            {
                var id = propInfoId.GetValue(entity);
                var entityPropertyValue            = entityPropertyPropInfo.GetValue(entity);
                RelatedEntityCollection collection = null;
                IDictionary             idDict     = null;
                object[] dictPropParameters        = new object[] { entityPropertyValue, null };
                if ((bool)tryGetValueMethod.Invoke(dict, dictPropParameters))
                {
                    object[] dictIdParams = new object[] { id, null };
                    if (!(bool)tryGetValueMethod2.Invoke((dictPropParameters[1] as IDictionary), dictIdParams))
                    {
                        collection = details.ToRelatedEntityCollection(id.ToString());
                        (dictPropParameters[1] as IDictionary).Add(id, collection);
                        list.Add(collection);
                    }
                    continue;
                }
                idDict     = Activator.CreateInstance(dictIdType) as IDictionary;
                collection = details.ToRelatedEntityCollection(id.ToString());
                idDict.Add(id, collection);
                list.Add(collection);
                dict.Add(entityPropertyValue, idDict);
            }
            foreach (var re in relatedEntities)
            {
                var value      = re?.Object?.GetValue(details.EntityToRelatedEntityProperty)?.ToString();
                var typedValue = value.ToType(entityPropertyPropInfo.PropertyType);
                if (dict[typedValue] is IDictionary idDict)
                {
                    foreach (var v in idDict.Values)
                    {
                        if (v is RelatedEntityCollection rec)
                        {
                            rec.RelatedEntities.Add(re);
                        }
                    }
                }
            }
            return(list);
        }
示例#3
0
 public List <RelatedEntityCollection> Sort(IEnumerable <T> entities, IEnumerable <RelatedEntity> relatedEntities, SortDetails details)
 {
     return(SortMethodDictionary[details.RelatedEntityType](entities, relatedEntities, details));
 }