Skip to content

fix:修复 Group 查询不支持 aot 问题 #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -87,6 +88,83 @@ public Select0Provider()
public abstract string ToSqlBase(string field = null);
public abstract void AsTableBase(Func<Type, string, string> tableRule);

internal virtual List<TReturn> ToListMrPrivate<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData)
{
var ret = new List<TReturn>();
if (_cancel?.Invoke() == true) return ret;
var dbParms = _params.ToArray();
var type = typeof(TReturn);
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var retCount = 0;
Exception exception = null;
try
{
_orm.Ado.ExecuteReader(_connection, _transaction, fetch =>
{
var index = -1;
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, fetch.Object, ref index, false, null, retCount, af.fillIncludeMany, af.fillSubSelectMany));
if (otherData != null)
foreach (var other in otherData)
other.retlist.Add(_commonExpression.ReadAnonymous(other.read, fetch.Object, ref index, false, null, retCount, null, null));
retCount++;
}, CommandType.Text, sql, _commandTimeout, dbParms);
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
_trackToList?.Invoke(ret);
return ret;
}
#region async
#if !net40
internal virtual async Task<List<TReturn>> ToListMrPrivateAsync<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData, CancellationToken cancellationToken)
{
var ret = new List<TReturn>();
if (_cancel?.Invoke() == true) return ret;
var dbParms = _params.ToArray();
var type = typeof(TReturn);
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var retCount = 0;
Exception exception = null;
try
{
await _orm.Ado.ExecuteReaderAsync(_connection, _transaction, fetch =>
{
var index = -1;
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, fetch.Object, ref index, false, null, retCount, af.fillIncludeMany, af.fillSubSelectMany));
if (otherData != null)
foreach (var other in otherData)
other.retlist.Add(_commonExpression.ReadAnonymous(other.read, fetch.Object, ref index, false, null, retCount, null, null));
retCount++;
return Task.FromResult(false);
}, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken);
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
_trackToList?.Invoke(ret);
return ret;
}
#endif
#endregion


public static void CopyData(Select0Provider from, Select0Provider to, ReadOnlyCollection<ParameterExpression> lambParms)
{
if (to == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,41 +343,10 @@ public Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(Func<T1, TKey> ke
return ret;
}

internal List<TReturn> ToListMrPrivate<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData)
internal override List<TReturn> ToListMrPrivate<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData)
{
var ret = new List<TReturn>();
if (_cancel?.Invoke() == true) return ret;
var dbParms = _params.ToArray();
var type = typeof(TReturn);
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var retCount = 0;
Exception exception = null;
try
{
_orm.Ado.ExecuteReader(_connection, _transaction, fetch =>
{
var index = -1;
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, fetch.Object, ref index, false, null, retCount, af.fillIncludeMany, af.fillSubSelectMany));
if (otherData != null)
foreach (var other in otherData)
other.retlist.Add(_commonExpression.ReadAnonymous(other.read, fetch.Object, ref index, false, null, retCount, null, null));
retCount++;
}, CommandType.Text, sql, _commandTimeout, dbParms);
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
if (typeof(TReturn) == typeof(T1))
foreach (var include in _includeToList) include?.Invoke(ret);
_trackToList?.Invoke(ret);
var ret = base.ToListMrPrivate<TReturn>(sql, af, otherData);
if (typeof(TReturn) == typeof(T1)) foreach (var include in _includeToList) include?.Invoke(ret);
return ret;
}
internal List<TReturn> ToListMapReaderPrivate<TReturn>(ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData)
Expand Down Expand Up @@ -1377,42 +1346,10 @@ await _orm.Ado.ExecuteReaderAsync(_connection, _transaction, fetch =>
return ret;
}

async internal Task<List<TReturn>> ToListMrPrivateAsync<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData, CancellationToken cancellationToken)
internal override async Task<List<TReturn>> ToListMrPrivateAsync<TReturn>(string sql, ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData, CancellationToken cancellationToken)
{
var ret = new List<TReturn>();
if (_cancel?.Invoke() == true) return ret;
var dbParms = _params.ToArray();
var type = typeof(TReturn);
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var retCount = 0;
Exception exception = null;
try
{
await _orm.Ado.ExecuteReaderAsync(_connection, _transaction, fetch =>
{
var index = -1;
ret.Add((TReturn)_commonExpression.ReadAnonymous(af.map, fetch.Object, ref index, false, null, retCount, af.fillIncludeMany, af.fillSubSelectMany));
if (otherData != null)
foreach (var other in otherData)
other.retlist.Add(_commonExpression.ReadAnonymous(other.read, fetch.Object, ref index, false, null, retCount, null, null));
retCount++;
return Task.FromResult(false);
}, CommandType.Text, sql, _commandTimeout, dbParms, cancellationToken);
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
if (typeof(TReturn) == typeof(T1))
foreach (var include in _includeToListAsync) await include?.Invoke(ret, cancellationToken);
_trackToList?.Invoke(ret);
var ret = await base.ToListMrPrivateAsync<TReturn>(sql, af, otherData, cancellationToken);
if (typeof(TReturn) == typeof(T1)) foreach (var include in _includeToListAsync) await include?.Invoke(ret, cancellationToken);
return ret;
}
internal Task<List<TReturn>> ToListMapReaderPrivateAsync<TReturn>(ReadAnonymousTypeAfInfo af, ReadAnonymousTypeOtherInfo[] otherData, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,6 @@ public void InternalOrderBy(Expression exp, bool isDescending)
var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { isDescending ? $"{sql} DESC" : sql, null });
}
public object InternalToList(Expression select, Type elementType)
{
var map = new ReadAnonymousTypeInfo();
var field = new StringBuilder();
var index = 0;

_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, select, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = elementType;
var method = _select.GetType().GetMethod("ToListMrPrivate", BindingFlags.Instance | BindingFlags.NonPublic);
method = method.MakeGenericMethod(elementType);
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
return method.Invoke(_select, new object[] { InternalToSql(fieldSql), new ReadAnonymousTypeAfInfo(map, fieldSql), null });
}
public IEnumerable<KeyValuePair<object, object>> InternalToKeyValuePairs(Expression elementSelector, Type elementType)
{
var map = new ReadAnonymousTypeInfo();
var field = new StringBuilder();
var index = 0;

_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, elementSelector, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = elementType;
var method = _select.GetType().GetMethod("ToListMrPrivate", BindingFlags.Instance | BindingFlags.NonPublic);
method = method.MakeGenericMethod(elementType);
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
var otherAf = new ReadAnonymousTypeOtherInfo(_field, _map, new List<object>());
var values = method.Invoke(_select, new object[] { InternalToSql($"{fieldSql}{_field}"), new ReadAnonymousTypeAfInfo(map, fieldSql), new[] { otherAf } }) as IList;
return otherAf.retlist.Select((a, b) => new KeyValuePair<object, object>(a, values[b]));
}
public string InternalToSql(Expression select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex)
{
var map = new ReadAnonymousTypeInfo();
Expand Down Expand Up @@ -214,8 +186,7 @@ public string InternalToSql(string field)
_select._skip = _groupBySkip;
break;
}
var method = _select.GetType().GetMethod("ToSql", new[] { typeof(string) });
var sql = method.Invoke(_select, new object[] { field }) as string;
var sql = _select.ToSqlBase(field);
if (isNestedPageSql == false)
{
_select._limit = 0;
Expand Down Expand Up @@ -337,13 +308,29 @@ public ISelectGrouping<TKey, TValue> OrderByDescending<TMember>(Expression<Func<
public TReturn First<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => ToList<TReturn>(select).FirstOrDefault();
public List<TReturn> ToList<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select)
{
var map = new ReadAnonymousTypeInfo();
var field = new StringBuilder();
var index = 0;

_lambdaParameter = select?.Parameters[0];
return InternalToList(select, typeof(TReturn)) as List<TReturn>;
_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, select, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = typeof(TReturn);
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
return _select.ToListMrPrivate<TReturn>(InternalToSql(fieldSql), new ReadAnonymousTypeAfInfo(map, fieldSql), null);
}
public Dictionary<TKey, TElement> ToDictionary<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector)
{
var map = new ReadAnonymousTypeInfo();
var field = new StringBuilder();
var index = 0;

_lambdaParameter = elementSelector?.Parameters[0];
return InternalToKeyValuePairs(elementSelector, typeof(TElement)).ToDictionary(a => (TKey)a.Key, a => (TElement)a.Value);
_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, elementSelector, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = typeof(TElement);
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
var otherAf = new ReadAnonymousTypeOtherInfo(_field, _map, new List<object>());
var values = _select.ToListMrPrivate<TElement>(InternalToSql($"{fieldSql}{_field}"), new ReadAnonymousTypeAfInfo(map, fieldSql), new[] { otherAf });
return otherAf.retlist.Select((a, b) => new KeyValuePair<TKey, TElement>((TKey)a, values[b])).ToDictionary(a=>a.Key,a=>a.Value);
}

#if net40
Expand All @@ -360,12 +347,10 @@ public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingA
_lambdaParameter = select?.Parameters[0];
_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, select, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = typeof(TReturn);
var method = _select.GetType().GetMethod("ToListMrPrivateAsync", BindingFlags.Instance | BindingFlags.NonPublic);
method = method.MakeGenericMethod(typeof(TReturn));
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
return method.Invoke(_select, new object[] { InternalToSql(fieldSql), new ReadAnonymousTypeAfInfo(map, fieldSql), null, cancellationToken }) as Task<List<TReturn>>;
return _select.ToListMrPrivateAsync<TReturn>(InternalToSql(fieldSql), new ReadAnonymousTypeAfInfo(map, fieldSql), null, cancellationToken);
}
async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector, CancellationToken cancellationToken = default)
public async Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector, CancellationToken cancellationToken = default)
{
var map = new ReadAnonymousTypeInfo();
var field = new StringBuilder();
Expand All @@ -374,11 +359,9 @@ async public Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expres
_lambdaParameter = elementSelector?.Parameters[0];
_comonExp.ReadAnonymousField(null, _select._tableRule, field, map, ref index, elementSelector, _select, this, null, null, null, false);
if (map.Childs.Any() == false && map.MapType == null) map.MapType = typeof(TElement);
var method = _select.GetType().GetMethod("ToListMrPrivateAsync", BindingFlags.Instance | BindingFlags.NonPublic);
method = method.MakeGenericMethod(typeof(TElement));
var fieldSql = field.Length > 0 ? field.Remove(0, 2).ToString() : null;
var otherAf = new ReadAnonymousTypeOtherInfo(_field, _map, new List<object>());
var values = await (method.Invoke(_select, new object[] { InternalToSql($"{fieldSql}{_field}"), new ReadAnonymousTypeAfInfo(map, fieldSql), new[] { otherAf }, cancellationToken }) as Task<List<TElement>>);
var values = await _select.ToListMrPrivateAsync<TElement>(InternalToSql($"{fieldSql}{_field}"), new ReadAnonymousTypeAfInfo(map, fieldSql), new[] { otherAf }, cancellationToken);
return otherAf.retlist.Select((a, b) => new KeyValuePair<TKey, TElement>((TKey)a, values[b])).ToDictionary(a => a.Key, a => a.Value);
}
#endif
Expand Down