/// <summary> /// 执行select many的自定义处理 /// </summary> /// <typeparam name="TLastItem"></typeparam> /// <param name="pretable"></param> public virtual void DoSelectMany <TLastItem>(LinqDLR2Sql <TLastItem> pretable) { _selectmanytables.AddRange(pretable._selectmanytables); if (pretable.Table != "") { _selectmanytables.Add(pretable); } _lastoperation = LinqDLR2SQLOperation.SelectMany; }
/// <summary> /// 执行select的自定义处理 /// </summary> /// <typeparam name="TLastItem"></typeparam> /// <param name="pretable"></param> public virtual void DoSelect <TLastItem>(LinqDLR2Sql <TLastItem> pretable) { if (pretable.LastOperation == LinqDLR2SQLOperation.SelectMany) { _selectmanytables.AddRange(pretable._selectmanytables); _lastoperation = LinqDLR2SQLOperation.SelectMany; _orderby = pretable._orderby; } else if (pretable.LastOperation == LinqDLR2SQLOperation.SelectJoin) { foreach (var item in pretable._jointables) { _jointables.Add(item.Key, item.Value); } _lastoperation = LinqDLR2SQLOperation.SelectJoin; _orderby = pretable._orderby; } else { _lastoperation = LinqDLR2SQLOperation.Select; _orderby = pretable._orderby; } _lastwhere = pretable.LastWhere; //生成sql if (LastOperation == LinqDLR2SQLOperation.Select) { var sql = "SELECT {prefix} {columns} FROM {tables} {alianname} {where} {orderby}"; var prefix = ""; var columns = GetColumnsSql(); var tables = Table; var alianname = string.IsNullOrEmpty(AliasName) ? "" : $"as {AliasName}"; var where = LastWhere == null ? "" : $"WHERE {LastWhere.Result}"; var orderby = _orderby == "" ? "" : $"ORDER BY {_orderby}"; sql = sql.Replace("{prefix}", prefix) .Replace("{columns}", columns) .Replace("{tables}", tables) .Replace("{alianname}", alianname) .Replace("{where}", where) .Replace("{orderby}", orderby); if (LastWhere != null) { foreach (var item in LastWhere.ConditionValues) { _conditionvalues.Add(item.Key, item.Value); } } _sql = sql; } else if (LastOperation == LinqDLR2SQLOperation.SelectMany) { var sql = "SELECT {prefix} {columns} FROM {tables} {where} {orderby}"; var prefix = ""; var columns = GetColumnsSql(); var tables = ""; foreach (LinqDLRTable table in SelectManyTables) { tables += $"{table.Table}{(string.IsNullOrEmpty(table.AliasName) ? "," : $" AS {table.AliasName},")}"; } tables = tables == "" ? "" : tables.Substring(0, tables.Length - 1); var where = LastWhere == null ? "" : $"WHERE {LastWhere.Result}"; var orderby = _orderby == "" ? "" : $"ORDER BY {_orderby}"; sql = sql.Replace("{prefix}", prefix) .Replace("{columns}", columns) .Replace("{tables}", tables) .Replace("{where}", where) .Replace("{orderby}", orderby); if (LastWhere != null) { foreach (var item in LastWhere.ConditionValues) { _conditionvalues.Add(item.Key, item.Value); } } _sql = sql; }