Beispiel #1
0
 public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List <SelectTableInfo> tables)
 {
     _orm      = orm;
     _select   = select;
     _map      = map;
     _field    = field;
     _comonExp = comonExp;
     _tables   = tables;
 }
Beispiel #2
0
        public static void CopyData(Select0Provider from, Select0Provider to, ReadOnlyCollection<ParameterExpression> lambParms)
        {
            if (to == null) return;
            to._limit = from._limit;
            to._skip = from._skip;
            to._select = from._select;
            to._orderby = from._orderby;
            to._groupby = from._groupby;
            to._having = from._having;
            to._where = new StringBuilder().Append(from._where.ToString());
            to._params = new List<DbParameter>(from._params.ToArray());

            if (lambParms == null)
                to._tables = new List<SelectTableInfo>(from._tables.ToArray());
            else
            {
                var findedIndexs = new List<int>();
                var _multiTables = to._tables;
                _multiTables[0] = from._tables[0];
                for (var a = 1; a < lambParms.Count; a++)
                {
                    var tbIndex = from._tables.FindIndex(b => b.Alias == lambParms[a].Name && b.Table.Type == lambParms[a].Type); ;
                    if (tbIndex != -1)
                    {
                        findedIndexs.Add(tbIndex);
                        _multiTables[a] = from._tables[tbIndex];
                    }
                    else
                    {
                        _multiTables[a].Alias = lambParms[a].Name;
                        _multiTables[a].Parameter = lambParms[a];
                    }
                }
                for (var a = 1; a < from._tables.Count; a++)
                {
                    if (findedIndexs.Contains(a)) continue;
                    _multiTables.Add(from._tables[a]);
                }
            }
            to._tableRules = new List<Func<Type, string, string>>(from._tableRules.ToArray());
            to._aliasRule = from._aliasRule;
            to._join = new StringBuilder().Append(from._join.ToString());
            //to._orm = from._orm;
            //to._commonUtils = from._commonUtils;
            //to._commonExpression = from._commonExpression;
            to._transaction = from._transaction;
            to._connection = from._connection;
            to._trackToList = from._trackToList;
            to._includeToList = new List<Action<object>>(from._includeToList.ToArray());
#if net40
#else
            to._includeToListAsync = new List<Func<object, Task>>(from._includeToListAsync.ToArray());
#endif
            to._distinct = from._distinct;
            to._selectExpression = from._selectExpression;
            to._whereGlobalFilter = new List<GlobalFilter.Item>(from._whereGlobalFilter.ToArray());
        }
        public SelectedQueryProvider(Select0Provider select, Expression selector)
        {
            _select   = select;
            _comonExp = _select._commonExpression;
            _map      = new ReadAnonymousTypeInfo();
            var field = new StringBuilder();
            var index = -10000; //临时规则,不返回 as1

            if (selector != null)
            {
                _comonExp.ReadAnonymousField(_select._tables, field, _map, ref index, selector, null, null, _select._whereGlobalFilter, null, false); //不走 DTO 映射,不处理 IncludeMany
            }
            _field = field.ToString();
        }
Beispiel #4
0
        public ISelect <TReturn> Select <TReturn>(Expression <Func <T1, TReturn> > select) where TReturn : class
        {
            if (typeof(TReturn) == typeof(T1))
            {
                return(this as ISelect <TReturn>);
            }
            _tables[0].Parameter = select.Parameters[0];
            _selectExpression    = select.Body;
            var ret = _orm.Select <TReturn>();

            Select0Provider <ISelect <T1>, T1> .CopyData(this, ret, null);

            return(ret);
        }
Beispiel #5
0
        public ISelect <TResult> SelectMany <TCollection, TResult>(Expression <Func <T1, ISelect <TCollection> > > collectionSelector, Expression <Func <T1, TCollection, TResult> > resultSelector) where TCollection : class where TResult : class
        {
            SelectTableInfo find = null;

            if (collectionSelector.Body.NodeType == ExpressionType.Call)
            {
                var callExp = collectionSelector.Body as MethodCallExpression;
                if (callExp.Method.Name == "DefaultIfEmpty" && callExp.Object.Type.GenericTypeArguments.Any())
                {
                    find = _tables.Where((a, idx) => idx > 0 && a.Type == SelectTableInfoType.InnerJoin && a.Table.Type == callExp.Object.Type.GenericTypeArguments[0]).LastOrDefault();
                    if (find != null)
                    {
                        if (!string.IsNullOrEmpty(find.On))
                        {
                            find.On = Regex.Replace(find.On, $@"\b{find.Alias}\.", $"{resultSelector.Parameters[1].Name}.");
                        }
                        if (!string.IsNullOrEmpty(find.NavigateCondition))
                        {
                            find.NavigateCondition = Regex.Replace(find.NavigateCondition, $@"\b{find.Alias}\.", $"{resultSelector.Parameters[1].Name}.");
                        }
                        find.Type      = SelectTableInfoType.LeftJoin;
                        find.Alias     = resultSelector.Parameters[1].Name;
                        find.Parameter = resultSelector.Parameters[1];
                    }
                }
            }
            if (find == null)
            {
                var tb = _commonUtils.GetTableByEntity(typeof(TCollection));
                if (tb == null)
                {
                    throw new Exception($"SelectMany 错误的类型:{typeof(TCollection).FullName}");
                }
                _tables.Add(new SelectTableInfo {
                    Alias = resultSelector.Parameters[1].Name, AliasInit = resultSelector.Parameters[1].Name, Parameter = resultSelector.Parameters[1], Table = tb, Type = SelectTableInfoType.From
                });
            }
            if (typeof(TResult) == typeof(T1))
            {
                return(this as ISelect <TResult>);
            }
            _selectExpression = resultSelector.Body;
            var ret = _orm.Select <TResult>() as Select1Provider <TResult>;

            Select0Provider <ISelect <T1>, T1> .CopyData(this, ret, null);

            return(ret);
        }
Beispiel #6
0
        public ISelect <TResult> GroupJoin <TInner, TKey, TResult>(ISelect <TInner> inner, Expression <Func <T1, TKey> > outerKeySelector, Expression <Func <TInner, TKey> > innerKeySelector, Expression <Func <T1, ISelect <TInner>, TResult> > resultSelector) where TInner : class where TResult : class
        {
            _tables[0].Parameter = resultSelector.Parameters[0];
            _commonExpression.ExpressionLambdaToSql(outerKeySelector, new CommonExpression.ExpTSC {
                _tables = _tables
            });
            this.InternalJoin(Expression.Lambda <Func <T1, TInner, bool> >(
                                  Expression.Equal(outerKeySelector.Body, innerKeySelector.Body),
                                  new[] { outerKeySelector.Parameters[0], innerKeySelector.Parameters[0] }
                                  ), SelectTableInfoType.InnerJoin);
            if (typeof(TResult) == typeof(T1))
            {
                return(this as ISelect <TResult>);
            }
            _selectExpression = resultSelector.Body;
            var ret = _orm.Select <TResult>() as Select1Provider <TResult>;

            Select0Provider <ISelect <T1>, T1> .CopyData(this, ret, null);

            return(ret);
        }