コード例 #1
0
        /// <summary>
        /// Get the first or default item using a predicate, order items, load related object using navigationProperties
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderby"></param>
        /// <param name="navigationProperties"></param>
        /// <returns></returns>
        public T FirstOrDefault(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby, params Expression <Func <T, object> >[] navigationProperties)
        {
            T ret = null;

            using (var context = (DbContext)Activator.CreateInstance(ContextType, ContextParameters))
            {
                IQueryable <T> dbQuery = context.Set <T>();
                //apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                //set the predicate
                if (predicate != null)
                {
                    dbQuery = dbQuery.AsNoTracking().Where(predicate);
                }

                if (orderby != null)
                {
                    //apply order
                    IOrderedQueryable <T> dbQueryOrdered = orderby.ApplyOrders(dbQuery);

                    ret = dbQueryOrdered.FirstOrDefault();
                }
                else
                {
                    ret = dbQuery.FirstOrDefault();
                }
            }
            return(ret);
        }
コード例 #2
0
        /// <summary>
        /// Get items using a predicate, order items and limit with an offset the selection, load related object using navigationProperties
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderby"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="navigationProperties"></param>
        /// <returns></returns>
        public virtual IList <T> List(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby, Nullable <int> skip, Nullable <int> take, params Expression <Func <T, object> >[] navigationProperties)
        {
            List <T> ret = new List <T>();

            using (var context = (DbContext)Activator.CreateInstance(ContextType, ContextParameters))
            {
                IQueryable <T> dbQuery = context.Set <T>();
                //apply eager loading
                foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include <T, object>(navigationProperty);
                }

                //set the predicate
                if (predicate != null)
                {
                    dbQuery = dbQuery.AsNoTracking().Where(predicate);
                }

                if (orderby != null)
                {
                    //apply order
                    IOrderedQueryable <T> dbQueryOrdered = orderby.ApplyOrders(dbQuery);

                    //limits
                    if (skip == null && take == null)
                    {
                        ret = dbQueryOrdered.ToList <T>();
                    }
                    else if (skip != null && take != null)
                    {
                        ret = dbQueryOrdered.Skip((int)skip).Take((int)take).ToList <T>();
                    }
                    else if (skip != null && take == null)
                    {
                        ret = dbQueryOrdered.Skip((int)skip).ToList <T>();
                    }
                    else if (skip == null && take != null)
                    {
                        ret = dbQueryOrdered.Take((int)take).ToList <T>();
                    }
                }
                else
                {
                    //limits
                    if (take != null)
                    {
                        ret = dbQuery.Take((int)take).ToList <T>();
                    }
                    else
                    {
                        ret = dbQuery.ToList <T>();
                    }
                }
            }
            return(ret);
        }
コード例 #3
0
        /// <summary>
        /// Apply order to query
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IOrderedQueryable <T> ApplyOrders(IQueryable <T> query)
        {
            //get the order list
            IGenericDataOrder <T>[] orderlist    = new IGenericDataOrder <T>[] { };
            IGenericDataOrder <T>   currentOrder = this;

            while (currentOrder != null)
            {
                orderlist = orderlist.Concat(new IGenericDataOrder <T>[] { currentOrder }).ToArray();
                IGenericDataOrder <T> parentOrder = currentOrder.GetParent();
                if (parentOrder != null)
                {
                    currentOrder = parentOrder;
                }
                else
                {
                    currentOrder = null;
                }
            }
            orderlist = orderlist.Reverse().ToArray();

            //build the ordered query
            IOrderedQueryable <T> queryOrdered = null;

            foreach (IGenericDataOrder <T> order in orderlist)
            {
                if (queryOrdered == null)
                {
                    if (order.SortAscending)
                    {
                        queryOrdered = order.ApplyOrderByAscending(query);
                    }
                    else
                    {
                        queryOrdered = order.ApplyOrderByDescending(query);
                    }
                }
                else
                {
                    if (order.SortAscending)
                    {
                        queryOrdered = order.ApplyThenByAscending(queryOrdered);
                    }
                    else
                    {
                        queryOrdered = order.ApplyThenByDescending(queryOrdered);
                    }
                }
            }

            return(queryOrdered);
        }
コード例 #4
0
        /// <summary>
        /// Get the parent IGenericDataOrder
        /// </summary>
        public IGenericDataOrder <T> GetParent()
        {
            IGenericDataOrder <T> parent = _caller as IGenericDataOrder <T>;

            if (parent != null)
            {
                return(parent);
            }
            else
            {
                return(null);
            }
        }
コード例 #5
0
 /// <summary>
 /// Get the first or default item, order items
 /// </summary>
 /// <param name="orderby"></param>
 /// <returns></returns>
 public T FirstOrDefault(IGenericDataOrder <T> orderby)
 {
     return(FirstOrDefault(null, orderby));
 }
コード例 #6
0
 /// <summary>
 /// Get the first or default item using a predicate, order items
 /// </summary>
 /// <param name="predicate"></param>
 /// <param name="orderby"></param>
 /// <returns></returns>
 public T FirstOrDefault(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby)
 {
     return(FirstOrDefault(predicate, orderby, new Expression <Func <T, object> >[] { }));
 }
コード例 #7
0
 /// <summary>
 /// Get items using a predicate, order items and limit with an offset the selection
 /// </summary>
 /// <param name="predicate"></param>
 /// <param name="orderby"></param>
 /// <param name="skip"></param>
 /// <param name="take"></param>
 /// <param name="navigationProperties"></param>
 /// <returns></returns>
 public IList <T> List(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby, Nullable <int> skip, Nullable <int> take)
 {
     return(List(predicate, orderby, skip, take, new Expression <Func <T, object> >[] { }));
 }
コード例 #8
0
 /// <summary>
 /// Get all items, order items
 /// </summary>
 /// <param name="orderby"></param>
 /// <returns></returns>
 public IList <T> List(IGenericDataOrder <T> orderby)
 {
     return(List(null, orderby, null, null));
 }
コード例 #9
0
 /// <summary>
 /// Get all items, order items and limit the selection
 /// </summary>
 /// <param name="orderby"></param>
 /// <param name="take"></param>
 /// <returns></returns>
 public IList <T> List(IGenericDataOrder <T> orderby, Nullable <int> take)
 {
     return(List(null, orderby, null, take));
 }
コード例 #10
0
 /// <summary>
 /// Get items using a predicate, order items
 /// </summary>
 /// <param name="predicate"></param>
 /// <param name="orderby"></param>
 /// <returns></returns>
 public IList <T> List(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby)
 {
     return(List(predicate, orderby, null, null));
 }
コード例 #11
0
 /// <summary>
 /// Get items using a predicate, order items and limit the selection
 /// </summary>
 /// <param name="predicate"></param>
 /// <param name="orderby"></param>
 /// <param name="take"></param>
 /// <returns></returns>
 public IList <T> List(Expression <Func <T, bool> > predicate, IGenericDataOrder <T> orderby, Nullable <int> take)
 {
     return(List(predicate, orderby, null, take));
 }