- 增加 ISelectGrouping First 方法;

This commit is contained in:
2881099
2023-03-02 19:14:09 +08:00
parent 4a4751708a
commit df3073819d
5 changed files with 32 additions and 9 deletions

View File

@ -16,6 +16,8 @@ namespace FreeSql
Task<long> CountAsync(CancellationToken cancellationToken = default);
Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, CancellationToken cancellationToken = default);
Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector, CancellationToken cancellationToken = default);
Task<TReturn> FirstAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, CancellationToken cancellationToken = default);
#endif
/// <summary>
@ -54,6 +56,13 @@ namespace FreeSql
/// <returns></returns>
List<TReturn> ToList<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select);
Dictionary<TKey, TElement> ToDictionary<TElement>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TElement>> elementSelector);
/// <summary>
/// 执行SQL查询返回指定字段的记录的第一条记录记录不存在时返回 TReturn 默认值
/// </summary>
/// <typeparam name="TReturn"></typeparam>
/// <param name="select"></param>
/// <returns></returns>
TReturn First<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select);
/// <summary>
/// 【linq to sql】专用方法不建议直接使用

View File

@ -1154,6 +1154,7 @@ namespace FreeSql.Internal.CommonProvider
var tbrefMid = _commonUtils.GetTableByEntity(tbref.RefMiddleEntityType);
var tbrefMidName = _tableRules?.FirstOrDefault()?.Invoke(tbref.RefMiddleEntityType, tbrefMid.DbName) ?? tbrefMid.DbName;
var sbJoin = new StringBuilder().Append($"{_commonUtils.QuoteSqlName(tbrefMidName)} midtb ON ");
if (_orm.CodeFirst.IsAutoSyncStructure && tbrefMid.Type != typeof(object)) _orm.CodeFirst.SyncStructure(tbrefMid.Type, tbrefMidName);
for (var z = 0; z < tbref.RefColumns.Count; z++)
{
if (z > 0) sbJoin.Append(" AND ");

View File

@ -329,6 +329,7 @@ namespace FreeSql.Internal.CommonProvider
}
public List<TReturn> Select<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select) => ToList(select);
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)
{
_lambdaParameter = select?.Parameters[0];
@ -344,6 +345,7 @@ namespace FreeSql.Internal.CommonProvider
#else
async public Task<long> CountAsync(CancellationToken cancellationToken = default) => _select._cancel?.Invoke() == true ? 0 : long.TryParse(string.Concat(await _orm.Ado.ExecuteScalarAsync(_select._connection, _select._transaction, CommandType.Text, $"select count(1) from ({this.ToSql($"1{_comonExp._common.FieldAsAlias("as1")}")}) fta", _select._commandTimeout, _select._params.ToArray(), cancellationToken)), out var trylng) ? trylng : default(long);
async public Task<TReturn> FirstAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, CancellationToken cancellationToken = default) => (await ToListAsync<TReturn>(select, cancellationToken)).FirstOrDefault();
public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TReturn>> select, CancellationToken cancellationToken = default)
{
var map = new ReadAnonymousTypeInfo();