- 增加 ISelect`1 ToDictionary 方法查询返回字典;

This commit is contained in:
28810 2020-03-20 01:52:43 +08:00
parent a5e6573851
commit 22a2450e05
17 changed files with 246 additions and 1 deletions

View File

@ -68,7 +68,7 @@ namespace FreeSql
internal void TrackToList(object list)
{
if (list == null) return;
var ls = list as IList<TEntity>;
var ls = list as IEnumerable<TEntity>;
if (ls == null)
{
var ie = list as IEnumerable;

View File

@ -251,6 +251,18 @@ namespace FreeSql.Tests.MySqlConnector
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.mysql.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -192,6 +192,18 @@ namespace FreeSql.Tests.Odbc.Dameng
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.dameng.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -183,6 +183,18 @@ namespace FreeSql.Tests.Odbc.Default
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.odbc.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -292,6 +292,18 @@ namespace FreeSql.Tests.Odbc.MySql
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.mysql.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -192,6 +192,18 @@ namespace FreeSql.Tests.Odbc.Oracle
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.oracle.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -268,6 +268,18 @@ namespace FreeSql.Tests.Odbc.PostgreSQL
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.pgsql.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -182,6 +182,18 @@ namespace FreeSql.Tests.Odbc.SqlServer
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.sqlserver.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -183,6 +183,20 @@ namespace FreeSql.Tests.MsAccess
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a=> new { a.Id, a.Title });
var testDto11 = select.Limit(10).ToDictionaryAsync(a => a.Id).Result;
var testDto22 = select.Limit(10).ToDictionaryAsync(a => a.Id, a => new { a.Id, a.Title }).Result;
var repo = g.msaccess.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -292,6 +292,18 @@ namespace FreeSql.Tests.MySql
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.mysql.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -192,6 +192,18 @@ namespace FreeSql.Tests.Oracle
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.oracle.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -268,6 +268,18 @@ namespace FreeSql.Tests.PostgreSQL
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.pgsql.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -192,6 +192,18 @@ namespace FreeSql.Tests.SqlServer
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.sqlserver.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -209,6 +209,18 @@ namespace FreeSql.Tests.Sqlite
var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
}
[Fact]
public void ToDictionary()
{
var testDto1 = select.Limit(10).ToDictionary(a => a.Id);
var testDto2 = select.Limit(10).ToDictionary(a => a.Id, a => new { a.Id, a.Title });
var repo = g.sqlite.GetRepository<Topic>();
var dic = repo.Select.Limit(10).ToDictionary(a => a.Id);
var first = dic.First().Value;
first.Clicks++;
repo.Update(first);
}
class TestGuidIdToList
{
public Guid id { get; set; }

View File

@ -1033,6 +1033,15 @@
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.ISelect0`2.ToDictionary``1(System.Func{`1,``0})">
<summary>
以字典的形式返回查询结果<para></para>
注意:字典的特点会导致 OrderBy 排序失效
</summary>
<typeparam name="TKey"></typeparam>
<param name="keySelector"></param>
<returns></returns>
</member>
<member name="M:FreeSql.ISelect0`2.ToList(System.Boolean)">
<summary>
执行SQL查询返回 T1 实体所有字段的记录,记录不存在时返回 Count 为 0 的列表<para></para>

View File

@ -14,6 +14,8 @@ namespace FreeSql
#if net40
#else
Task<DataTable> ToDataTableAsync(string field = null);
Task<Dictionary<TKey, T1>> ToDictionaryAsync<TKey>(Func<T1, TKey> keySelector);
Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TKey, TValue>(Func<T1, TKey> keySelector, Func<T1, TValue> valueSelector);
Task<List<T1>> ToListAsync(bool includeNestedMembers = false);
Task<List<TTuple>> ToListAsync<TTuple>(string field);
@ -50,6 +52,15 @@ namespace FreeSql
/// <returns></returns>
DataTable ToDataTable(string field = null);
/// <summary>
/// 以字典的形式返回查询结果<para></para>
/// 注意:字典的特点会导致 OrderBy 排序失效
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="keySelector"></param>
/// <returns></returns>
Dictionary<TKey, T1> ToDictionary<TKey>(Func<T1, TKey> keySelector);
Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(Func<T1, TKey> keySelector, Func<T1, TValue> valueSelector);
/// <summary>
/// 执行SQL查询返回 T1 实体所有字段的记录,记录不存在时返回 Count 为 0 的列表<para></para>
/// 注意:<para></para>

View File

@ -479,6 +479,39 @@ namespace FreeSql.Internal.CommonProvider
this.ToListChunkPrivate(size, done, includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevelAll(), null);
}
#endregion
public Dictionary<TKey, T1> ToDictionary<TKey>(Func<T1, TKey> keySelector) => ToDictionary(keySelector, a => a);
public Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(Func<T1, TKey> keySelector, Func<T1, TValue> valueSelector)
{
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
if (valueSelector == null) throw new ArgumentNullException(nameof(valueSelector));
var af = this.GetAllFieldExpressionTreeLevel2();
var sql = this.ToSql(af.Field);
var dbParms = _params.ToArray();
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var ret = new Dictionary<TKey, TValue>();
Exception exception = null;
try
{
_orm.Ado.ExecuteReader(_connection, _transaction, dr =>
{
var item = af.Read(_orm, dr);
ret.Add(keySelector(item), valueSelector(item));
}, CommandType.Text, sql, dbParms);
}
catch (Exception ex)
{
exception = ex;
throw ex;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
if (typeof(TValue) == typeof(T1)) _trackToList?.Invoke(ret.Values);
return ret;
}
public virtual List<T1> ToList(bool includeNestedMembers = false)
{
if (_selectExpression != null) return this.InternalToList<T1>(_selectExpression);
@ -1251,6 +1284,40 @@ namespace FreeSql.Internal.CommonProvider
return ToListAfPrivateAsync(sql, af, otherData);
}
public Task<Dictionary<TKey, T1>> ToDictionaryAsync<TKey>(Func<T1, TKey> keySelector) => ToDictionaryAsync(keySelector, a => a);
async public Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TKey, TValue>(Func<T1, TKey> keySelector, Func<T1, TValue> valueSelector)
{
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
if (valueSelector == null) throw new ArgumentNullException(nameof(valueSelector));
var af = this.GetAllFieldExpressionTreeLevel2();
var sql = this.ToSql(af.Field);
var dbParms = _params.ToArray();
var before = new Aop.CurdBeforeEventArgs(_tables[0].Table.Type, _tables[0].Table, Aop.CurdType.Select, sql, dbParms);
_orm.Aop.CurdBeforeHandler?.Invoke(this, before);
var ret = new Dictionary<TKey, TValue>();
Exception exception = null;
try
{
await _orm.Ado.ExecuteReaderAsync(_connection, _transaction, dr =>
{
var item = af.Read(_orm, dr);
ret.Add(keySelector(item), valueSelector(item));
return Task.FromResult(false);
}, CommandType.Text, sql, dbParms);
}
catch (Exception ex)
{
exception = ex;
throw ex;
}
finally
{
var after = new Aop.CurdAfterEventArgs(before, exception, ret);
_orm.Aop.CurdAfterHandler?.Invoke(this, after);
}
if (typeof(TValue) == typeof(T1)) _trackToList?.Invoke(ret.Values);
return ret;
}
public virtual Task<List<T1>> ToListAsync(bool includeNestedMembers = false)
{
if (_selectExpression != null) return this.InternalToListAsync<T1>(_selectExpression);