修复 .ToList(a => a.id) 当 id 是 guid 类型时,会出现类型转换失败 bug;

This commit is contained in:
28810 2019-04-11 12:30:54 +08:00
parent 8da9474013
commit b5c79204d8
8 changed files with 52 additions and 7 deletions

View File

@ -230,6 +230,14 @@ namespace FreeSql.Tests.MySql {
var t1111 = g.mysql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type }); var t1111 = g.mysql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type });
var t2222 = g.mysql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type.Name }); var t2222 = g.mysql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type.Name });
g.mysql.Insert<TestGuidIdToList>().AppendData(new TestGuidIdToList()).ExecuteAffrows();
var testGuidId5 = g.mysql.Select<TestGuidIdToList>().ToList();
var testGuidId6 = g.mysql.Select<TestGuidIdToList>().ToList(a => a.id);
}
class TestGuidIdToList {
public Guid id { get; set; }
public string title { get; set; } = Guid.NewGuid().ToString();
} }
[Fact] [Fact]
public void ToOne() { public void ToOne() {

View File

@ -139,6 +139,14 @@ namespace FreeSql.Tests.Oracle {
var testDto2 = select.Limit(10).ToList(a => new TestDto()); var testDto2 = select.Limit(10).ToList(a => new TestDto());
var testDto3 = select.Limit(10).ToList(a => new TestDto { }); var testDto3 = select.Limit(10).ToList(a => new TestDto { });
var testDto4 = select.Limit(10).ToList(a => new TestDto() { }); var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
g.oracle.Insert<TestGuidIdToList>().AppendData(new TestGuidIdToList()).ExecuteAffrows();
var testGuidId5 = g.oracle.Select<TestGuidIdToList>().ToList();
var testGuidId6 = g.oracle.Select<TestGuidIdToList>().ToList(a => a.id);
}
class TestGuidIdToList {
public Guid id { get; set; }
public string title { get; set; } = Guid.NewGuid().ToString();
} }
[Fact] [Fact]
public void ToOne() { public void ToOne() {

View File

@ -209,6 +209,14 @@ namespace FreeSql.Tests.PostgreSQL {
var t1111 = g.pgsql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type }); var t1111 = g.pgsql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type });
var t2222 = g.pgsql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type.Name }); var t2222 = g.pgsql.Select<TestInfo>().ToList(a => new { a.Id, a.Title, a.Type.Name });
g.pgsql.Insert<TestGuidIdToList>().AppendData(new TestGuidIdToList()).ExecuteAffrows();
var testGuidId5 = g.pgsql.Select<TestGuidIdToList>().ToList();
var testGuidId6 = g.pgsql.Select<TestGuidIdToList>().ToList(a => a.id);
}
class TestGuidIdToList {
public Guid id { get; set; }
public string title { get; set; } = Guid.NewGuid().ToString();
} }
[Fact] [Fact]
public void ToOne() { public void ToOne() {

View File

@ -51,6 +51,9 @@ namespace FreeSql.Tests.PostgreSQLExpression {
var sql112222 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList(); var sql112222 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList(); var sql113333 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();
var sql1111112 = select.ToList(a => inarray);
var sql1111113 = select.ToList(a => a.testFieldIntArray);
var sql3 = select.Where(a => a.testFieldIntArray.Any()).ToList(); var sql3 = select.Where(a => a.testFieldIntArray.Any()).ToList();
var sql4 = select.Where(a => a.testFieldIntArray.Any() == false).ToList(); var sql4 = select.Where(a => a.testFieldIntArray.Any() == false).ToList();

View File

@ -141,6 +141,14 @@ namespace FreeSql.Tests.SqlServer {
var testDto2 = select.Limit(10).ToList(a => new TestDto()); var testDto2 = select.Limit(10).ToList(a => new TestDto());
var testDto3 = select.Limit(10).ToList(a => new TestDto { }); var testDto3 = select.Limit(10).ToList(a => new TestDto { });
var testDto4 = select.Limit(10).ToList(a => new TestDto() { }); var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
_sqlserverFixture.SqlServer.Insert<TestGuidIdToList>().AppendData(new TestGuidIdToList()).ExecuteAffrows();
var testGuidId5 = _sqlserverFixture.SqlServer.Select<TestGuidIdToList>().ToList();
var testGuidId6 = _sqlserverFixture.SqlServer.Select<TestGuidIdToList>().ToList(a => a.id);
}
class TestGuidIdToList {
public Guid id { get; set; }
public string title { get; set; } = Guid.NewGuid().ToString();
} }
[Fact] [Fact]
public void ToOne() { public void ToOne() {

View File

@ -133,6 +133,14 @@ namespace FreeSql.Tests.Sqlite {
var testDto2 = select.Limit(10).ToList(a => new TestDto()); var testDto2 = select.Limit(10).ToList(a => new TestDto());
var testDto3 = select.Limit(10).ToList(a => new TestDto { }); var testDto3 = select.Limit(10).ToList(a => new TestDto { });
var testDto4 = select.Limit(10).ToList(a => new TestDto() { }); var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
g.sqlite.Insert<TestGuidIdToList>().AppendData(new TestGuidIdToList()).ExecuteAffrows();
var testGuidId5 = g.sqlite.Select<TestGuidIdToList>().ToList();
var testGuidId6 = g.sqlite.Select<TestGuidIdToList>().ToList(a => a.id);
}
class TestGuidIdToList {
public Guid id { get; set; }
public string title { get; set; } = Guid.NewGuid().ToString();
} }
[Fact] [Fact]
public void ToOne() { public void ToOne() {

View File

@ -66,13 +66,15 @@ namespace FreeSql.Internal {
for (var idx = 0; idx < map.Count; idx++) { for (var idx = 0; idx < map.Count; idx++) {
var child = new ReadAnonymousTypeInfo { var child = new ReadAnonymousTypeInfo {
Property = tb.Properties.TryGetValue(map[idx].Column.CsName, out var tryprop) ? tryprop : tb.Type.GetProperty(map[idx].Column.CsName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance), Property = tb.Properties.TryGetValue(map[idx].Column.CsName, out var tryprop) ? tryprop : tb.Type.GetProperty(map[idx].Column.CsName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance),
CsName = map[idx].Column.CsName, DbField = $"{map[idx].Table.Alias}.{_common.QuoteSqlName(map[idx].Column.Attribute.Name)}" CsName = map[idx].Column.CsName, DbField = $"{map[idx].Table.Alias}.{_common.QuoteSqlName(map[idx].Column.Attribute.Name)}",
CsType = map[idx].Column.CsType
}; };
field.Append(", ").Append(_common.QuoteReadColumn(map[idx].Column.CsType, child.DbField)); field.Append(", ").Append(_common.QuoteReadColumn(map[idx].Column.CsType, child.DbField));
if (index >= 0) field.Append(" as").Append(++index); if (index >= 0) field.Append(" as").Append(++index);
parent.Childs.Add(child); parent.Childs.Add(child);
} }
} else { } else {
parent.CsType = exp.Type;
parent.DbField = ExpressionLambdaToSql(exp, _tables, null, getSelectGroupingMapString, SelectTableInfoType.From, true, false, ExpressionStyle.Where); parent.DbField = ExpressionLambdaToSql(exp, _tables, null, getSelectGroupingMapString, SelectTableInfoType.From, true, false, ExpressionStyle.Where);
field.Append(", ").Append(parent.DbField); field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(" as").Append(++index); if (index >= 0) field.Append(" as").Append(++index);
@ -157,9 +159,9 @@ namespace FreeSql.Internal {
if (parent.Childs.Any() == false) { if (parent.Childs.Any() == false) {
if (notRead) { if (notRead) {
++index; ++index;
return null; return Utils.GetDataReaderValue(parent.CsType, null);
} }
return dr.GetValue(++index); return Utils.GetDataReaderValue(parent.CsType, dr.GetValue(++index));
} }
switch (parent.ConsturctorType) { switch (parent.ConsturctorType) {
case ReadAnonymousTypeInfoConsturctorType.Arguments: case ReadAnonymousTypeInfoConsturctorType.Arguments:
@ -167,7 +169,7 @@ namespace FreeSql.Internal {
for (var a = 0; a < parent.Childs.Count; a++) { for (var a = 0; a < parent.Childs.Count; a++) {
var objval = ReadAnonymous(parent.Childs[a], dr, ref index, notRead); var objval = ReadAnonymous(parent.Childs[a], dr, ref index, notRead);
if (notRead == false) if (notRead == false)
args[a] = Utils.GetDataReaderValue(parent.Childs[a].CsType, objval); args[a] = objval;
} }
return parent.Consturctor.Invoke(args); return parent.Consturctor.Invoke(args);
case ReadAnonymousTypeInfoConsturctorType.Properties: case ReadAnonymousTypeInfoConsturctorType.Properties:
@ -176,11 +178,10 @@ namespace FreeSql.Internal {
for (var b = 0; b < parent.Childs.Count; b++) { for (var b = 0; b < parent.Childs.Count; b++) {
var prop = parent.Childs[b].Property; var prop = parent.Childs[b].Property;
var objval = ReadAnonymous(parent.Childs[b], dr, ref index, notRead); var objval = ReadAnonymous(parent.Childs[b], dr, ref index, notRead);
var safeval = Utils.GetDataReaderValue(prop.PropertyType, objval); if (isnull == false && objval == null && parent.Table != null && parent.Table.ColumnsByCs.TryGetValue(parent.Childs[b].CsName, out var trycol) && trycol.Attribute.IsPrimary)
if (isnull == false && safeval == null && parent.Table != null && parent.Table.ColumnsByCs.TryGetValue(parent.Childs[b].CsName, out var trycol) && trycol.Attribute.IsPrimary)
isnull = true; isnull = true;
if (isnull == false) if (isnull == false)
prop.SetValue(ret, safeval, null); prop.SetValue(ret, objval, null);
} }
return isnull ? null : ret; return isnull ? null : ret;
} }

View File

@ -1098,6 +1098,7 @@ namespace FreeSql.Internal {
} }
public static object GetDataReaderValue(Type type, object value) { public static object GetDataReaderValue(Type type, object value) {
if (value == null || value == DBNull.Value) return null; if (value == null || value == DBNull.Value) return null;
if (type == null) return value;
var func = _dicGetDataReaderValue.GetOrAdd(type, k1 => new ConcurrentDictionary<Type, Func<object, object>>()).GetOrAdd(value.GetType(), valueType => { var func = _dicGetDataReaderValue.GetOrAdd(type, k1 => new ConcurrentDictionary<Type, Func<object, object>>()).GetOrAdd(value.GetType(), valueType => {
var parmExp = Expression.Parameter(typeof(object), "value"); var parmExp = Expression.Parameter(typeof(object), "value");
var exp = GetDataReaderValueBlockExpression(type, parmExp); var exp = GetDataReaderValueBlockExpression(type, parmExp);