예제 #1
0
        public static IQueryable <T> OrderBy <T>(this IQueryable <T> query, List <IDTO_Sort> idtoSorts) where T : DBEntity_Basic
        {
            Type sourceType = typeof(T);

            PropertyInfo[] sourcePropertyInfos = sourceType.GetProperties();

            if (idtoSorts == null)
            {
                idtoSorts = new List <IDTO_Sort>();
            }

            if (idtoSorts.Count == 0)
            {
                var attr = sourcePropertyInfos.GetDefaultSortField <T>();

                idtoSorts.Add(new IDTO_Sort()
                {
                    FieldName = attr.RealName, EDirection = attr.EDirection
                });
            }

            Type       classType = typeof(IDTO_Sort_Ext);
            MethodInfo mi        = classType.GetMethod("AppendExpression");

            bool firstAdd = true;

            foreach (var sortItem in idtoSorts)
            {
                if (Verification.IsEmptyString(sortItem.FieldName))
                {
                    continue;
                }

                var sortFieldInfo = sourcePropertyInfos.GetSortField <T>(sortItem);
                if (sortFieldInfo == null)
                {
                    continue;
                }

                query = mi.MakeGenericMethod(new[] { sourceType, sortFieldInfo.Item1.PropertyType })
                        .Invoke(null, new object[] { query, sortItem.EDirection, firstAdd, sourceType, sortFieldInfo.Item2 }) as IQueryable <T>;

                firstAdd = false;
            }

            return(query);
        }