Exemplo n.º 1
0
        private static List <SelectListItem> GetSelectedListItems(
            PropertyMetaData property, System.Collections.IEnumerable source)
        {
            var          result = new List <SelectListItem>();
            var          relatedPropertyType = property.ForeignType();
            var          foreignMetaData     = MetaDataProvider.Get(relatedPropertyType);
            var          first        = true;
            PropertyInfo propertyInfo = null;

            foreach (var o in source)
            {
                object id;
                if (first)
                {
                    propertyInfo = o.GetType().GetProperty(property.Info.Name);
                    first        = false;
                }
                if (propertyInfo != null)
                {
                    id = o.GetValue(property.Info.Name);
                }
                else
                {
                    id = o.GetValue(LinqToSqlUtils.GetPKPropertyName(
                                        foreignMetaData.EntityType));
                }
                result.Add(new SelectListItem
                {
                    Text  = GetDisplay(o, foreignMetaData).ToString(),
                    Value = id.ToString()
                });
            }

            return(result.OrderBy(sli => sli.Text).ToList());
        }
Exemplo n.º 2
0
            public static Expression <Func <DataContext, object, K> > GetExpression()
            {
                var _type         = typeof(K);
                var itemParameter = Expression.Parameter(_type, "item");
                var pkParameter   = Expression.Parameter(typeof(object), "pk");
                var dcParameter   = Expression.Parameter(typeof(DataContext), "dc");
                Expression <Func <IQueryable <K>, K> >       exp  = x => x.FirstOrDefault(y => true);
                Expression <Func <DataContext, Table <K> > > exp2 = x => x.GetTable <K>();

                var firstOrDefaultInfo = ((MethodCallExpression)exp.Body).Method;
                var getTableInfo       = ((MethodCallExpression)exp2.Body).Method;


                var pkPropertyName = LinqToSqlUtils.GetPKPropertyName(_type);
                var pkProperty     = Expression.Property(
                    itemParameter,
                    pkPropertyName
                    );

                return(Expression.Lambda <Func <DataContext, object, K> >(
                           Expression.Call(null,
                                           firstOrDefaultInfo,
                                           Expression.Call(dcParameter, getTableInfo), Expression.Lambda <Func <K, bool> >
                                           (
                                               Expression.Equal(
                                                   Expression.Convert(pkProperty, typeof(object)),
                                                   pkParameter
                                                   ),
                                               new[] { itemParameter }
                                           )), new[] { dcParameter, pkParameter }));
            }
Exemplo n.º 3
0
        public virtual IQueryable GetComboBoxSource(
            PropertyMetaData propertyMetaData, bool onlyActive, string filter)
        {
            var relatedMeta = MetaDataProvider.Get(
                propertyMetaData.ForeignType());
            var idProperty = LinqToSqlUtils.GetPKPropertyName(relatedMeta.EntityType);
            var selector   = "new(" + idProperty +
                             " as " + propertyMetaData.Info.Name +
                             ", " + relatedMeta.DisplayProperty().Name + ")";

            var result =
                DynamicRepository.GetAll(propertyMetaData.ForeignType());

            if (filter != null)
            {
                result = result.Where(filter);
            }

            /*       if(onlyActive)
             *         foreach (var property in Const.Common.ActiveProperties)
             *         {
             *             if(propertyMetaData.ForeignType.HasProperty(property))
             *                 result = result.Where(property);
             *         }*/

            return(result.Select(selector));
        }
Exemplo n.º 4
0
        public virtual ActionResult List(int pageIndex, OrderColumn orderColumn)
        {
            var currentFilterValues = GetCurrentFilterValues();
            var items = AddFilterToList(currentFilterValues);

            if (orderColumn.ColumnName.IsEmpty())
            {
                if (MetaData.EntityType.HasProperty(UpdateDate))
                {
                    orderColumn = new OrderColumn {
                        IsDesc     = true,
                        ColumnName = UpdateDate,
                    };
                }
                else
                {
                    orderColumn = new OrderColumn {
                        IsDesc     = true,
                        ColumnName =
                            LinqToSqlUtils.GetPKPropertyName(MetaData.EntityType)
                    }
                };
            }



            if (!orderColumn.ColumnName.IsEmpty())
            {
                var orderQuery = orderColumn.ColumnName;
                if (orderColumn.IsDesc)
                {
                    orderQuery += " " + Const.Common.Descending;
                }
                items = items.OrderBy(orderQuery);
            }
            items = OnListSelecting(items);
            var tableData = new List <ListVM.Row>();
            var pagedList = items.ToPagedList(pageIndex - 1);

            foreach (var item in pagedList)
            {
                var id   = LinqToSqlUtils.GetPK(item);
                var list = GetValuesForRow(item);
                tableData.Add(new ListVM.Row {
                    Id = id, Entity = item, Values = list
                });
            }

            var listVM = new ListVM(MetaData, pagedList, tableData);

            AddExtraControls(listVM, true);
            listVM.FilterValues = currentFilterValues;
            listVM.OrderColumn  = orderColumn;

            ListVMCreated(listVM);

            return(View(listVM));
        }
Exemplo n.º 5
0
        public virtual IQueryable GetSource(Type entityType)
        {
            var relatedMeta = MetaDataProvider.Get(entityType);
            var idProperty  = LinqToSqlUtils.GetPKPropertyName(relatedMeta.EntityType);
            var selector    = "new(" + idProperty +
                              ", " + relatedMeta.DisplayProperty().Name + ")";

            return(DynamicRepository.GetAll(
                       entityType)
                   .Select(selector));
        }
Exemplo n.º 6
0
        public static T ByPrimeryKey <T>(this IQueryable <T> queryable,
                                         object primeryKey)
        {
            if (primeryKey == null)
            {
                return(default(T));
            }
            var type = typeof(T);

            return(queryable.Where(LinqToSqlUtils.GetPKPropertyName(type) +
                                   " = @0", primeryKey).FirstOrDefault());
        }
Exemplo n.º 7
0
        public static object GetObject(Type type, object id)
        {
            var constructor    = type.GetConstructors()[0];
            var obj            = constructor.Invoke(null);
            var publicPIs      = type.GetProperties();
            var associationPIs = publicPIs.Where(pi => pi.HasAttribute(typeof(AssociationAttribute)));
            var thisKeys       = associationPIs.Select(pi => pi.GetAttribute <AssociationAttribute>().ThisKey);
            var idPropertyname = LinqToSqlUtils.GetPKPropertyName(type);

            foreach (var propertyInfo in publicPIs)
            {
                object value = null;
                if (propertyInfo.Name == idPropertyname)
                {
                    value = Convert.ChangeType(id, propertyInfo.PropertyType);
                }
                else if (thisKeys.Contains(propertyInfo.Name))
                {
                    continue;
                }
                else if (propertyInfo.PropertyType == typeof(string))
                {
                    value = propertyInfo.Name + id;
                }

                if (propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(obj, value, new object[0]);
                }
            }

            var foreignKeyPIs = associationPIs.Where(pi => pi.GetAttribute <AssociationAttribute>().IsForeignKey);

            foreach (var propertyInfo in foreignKeyPIs)
            {
                if (propertyInfo.PropertyType == type)
                {
                    continue;
                }
                var idPI = LinqToSqlUtils.GetPKPropertyInfo(propertyInfo.PropertyType);
                if (propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(obj, GetObject(propertyInfo.PropertyType,
                                                         GetDefaultId(idPI.PropertyType)), new object[0]);
                }
            }

            return(obj);
        }
Exemplo n.º 8
0
        public virtual IQueryable <T> GetByPK(IEnumerable <object> pkList)
        {
            if (!pkList.Any())
            {
                return(Enumerable.Empty <T>().AsQueryable());
            }

            var idPropertyName = LinqToSqlUtils.GetPKPropertyName(_type);

            var where = pkList.Select((pk, index) =>
                                      string.Format(" {0} == @{1} ", idPropertyName, index))
                        .JoinWith(" || ");

            return(GetAll().Where(where, pkList.ToArray()));

//            return GetAll().Where(LinqToSqlUtil.WhereContains<T>(pkList.AsQueryable()));
        }
Exemplo n.º 9
0
        public string GetSelector(PropertyMetaData propertyMetaData,
                                  bool withoutProperty)
        {
            var relatedMeta = MetaDataProvider.Get(propertyMetaData.ForeignType());
            var property    = string.Empty;

            if (!withoutProperty && propertyMetaData.ForeignInfo() != null)
            {
                property = propertyMetaData.ForeignInfo().Name + ".";
            }
            var idProperty = property
                             + LinqToSqlUtils.GetPKPropertyName(relatedMeta.EntityType);

            return("new(" + idProperty +
                   " as " + propertyMetaData.Info.Name +
                   ", " + property +
                   relatedMeta.DisplayProperty().Name + ")");
        }
Exemplo n.º 10
0
        public virtual T GetByPK(object pk)
        {
            /*return context.GetTable<T>().Where(LinqToSqlUtil.GetIdPropertyName(_type) +
             *  " = @0", id).FirstOrDefault();*/
            var itemParameter = Expression.Parameter(_type, "item");

            var whereExpression = Expression.Lambda <Func <T, bool> >
                                  (
                Expression.Equal(
                    Expression.Property(
                        itemParameter,
                        LinqToSqlUtils.GetPKPropertyName(_type)
                        ),
                    Expression.Constant(pk)
                    ),
                new[] { itemParameter }
                                  );

            return(GetAll().FirstOrDefault(whereExpression));
        }
Exemplo n.º 11
0
        private void AddFilterValueToSource(PropertyMetaData propertyMetaData,
                                            object filterValue, ICollection <object> source)
        {
            var nameValue = source.Where(o => o.GetValue(propertyMetaData.Info.Name)
                                         .Equals(filterValue));

            if (nameValue.Count() == 0)
            {
                var foreignType = propertyMetaData.ForeignType();
//                if(propertyMetaData.ForeignInfo() != null)
                var item = DynamicRepository.GetAll(
                    foreignType)
                           .Where(LinqToSqlUtils
                                  .GetPKPropertyName(foreignType) + "= @0", filterValue)
                           .Select(GetSelector(propertyMetaData, true))
                           .Cast <object>().First();

/*                else {
 *                  item = DynamicRepository.GetByPK( foreignType, filterValue); }*/

                source.Add(item);
            }
        }
Exemplo n.º 12
0
                                                                      public static IQueryable <T> GetByPKList(IQueryable <T> list,
                                                                                                               IEnumerable <object> pkList)
                                                                      {
                                                                          var idPropertyName = LinqToSqlUtils.GetPKPropertyName(typeof(T));

                                                                          var where = pkList.Select((pk, index) =>
                                                                                                    string.Format("{0} == @{1} ", idPropertyName, index))
                                                                                      .JoinWith(" || ");
                                                                          return(list.Where(where, pkList.ToArray()));
                                                                      }