Esempio n. 1
0
        public static Result <List <TEntity> > QueryListSimple <TEntity>(DbContext CurrContext, QueryCondition condition)
        {
            Result <List <TEntity> > result = new Result <List <TEntity> >();

            try
            {
                if (condition.PageInfo != null && condition.PageInfo.OrderAndSortList != "" && condition.SortList.Count == 0)
                {
                    condition.CheckOrderAndSortList();
                }

                ObjectContext objectContext   = ((IObjectContextAdapter)CurrContext).ObjectContext;
                string        ContextTypeName = CurrContext.GetType().Name;
                string        TableName       = typeof(TEntity).Name;

                bool isAllowPage = false;
                if (condition != null && condition.PageInfo != null && condition.PageInfo.isAllowPage)
                {
                    isAllowPage = true;

                    //如果没有SORT条件;则需要随意增加一个默认按照关键字的条件;

                    if (condition.SortList.Count == 0)
                    {
                        string[] KeyFieldList = FindKeyFieldList <TEntity>(CurrContext);

                        if (KeyFieldList.Length != 0)
                        {
                            string _keyName = KeyFieldList[0];
                            condition.SortList.Add(new SortExpression(TableName + "." + _keyName, eSortType.Desc));
                        }
                    }
                }



                /*构造ESQL*/
                string ESQL = "SELECT value " + TableName + " FROM " + ContextTypeName + "." + TableName;

                //获取条件;
                ESQL = ESQL + GetWhereESQL(TableName, condition);

                //增加ORDER BY;
                ESQL = ESQL + GetOrderESQL(TableName, condition);

                IQueryable <TEntity>  query       = objectContext.CreateQuery <TEntity>(ESQL);
                ObjectQuery <TEntity> objectquery = objectContext.CreateQuery <TEntity>(ESQL);


                objectquery.MergeOption = MergeOption.NoTracking;

                if (isAllowPage)
                {
                    query = objectquery.Skip <TEntity>((condition.PageInfo.CurrentPageIndex - 1) *
                                                       condition.PageInfo.PageRowCount).Take <TEntity>(condition.PageInfo.PageRowCount);
                    //当前页的数据;
                    result.Data = query.ToList <TEntity>();
                    //总行数;此语句已经转化为COUNT来处理;
                    result.AllRowsCount = objectquery.Count();
                }
                else
                {
                    query = objectquery;
                    //当前页的数据;
                    result.Data = query.ToList <TEntity>();
                    //总行数;
                    result.AllRowsCount = result.Data.Count;
                }
                result.Flag = EResultFlag.Success;
            }
            catch (Exception ex)
            {
                result.Exception    = new ExceptionEx(ex, "QueryByCondition");
                result.AllRowsCount = -1;
                result.Data         = null;
                result.Flag         = EResultFlag.Failure;
            }
            return(result);
        }