Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c2e88e8

Browse files
committed
When selecting using an anonymous type to specify column the anonymous type's property name is now used as the column alias. This allows working with ambiguous column names when joining multiple tables.
1 parent 5d30055 commit c2e88e8

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,13 +1449,22 @@ protected virtual object VisitNew(NewExpression nex)
14491449
if (isAnonType)
14501450
{
14511451
var exprs = VisitExpressionList(nex.Arguments);
1452+
14521453
var r = StringBuilderCache.Allocate();
1453-
foreach (object e in exprs)
1454+
for (var i = 0; i < exprs.Count; ++i)
14541455
{
1455-
if (r.Length > 0)
1456+
if (i > 0)
14561457
r.Append(",");
14571458

1458-
r.Append(e);
1459+
r.Append(exprs[i]);
1460+
1461+
// Use the anon type property name, rather than the table property name, as the returned column name
1462+
1463+
var propertyExpr = nex.Arguments[i] as MemberExpression;
1464+
if (propertyExpr != null && propertyExpr.Member.Name != nex.Members[i].Name)
1465+
{
1466+
r.Append(" AS " + DialectProvider.GetQuotedName(nex.Members[i].Name));
1467+
}
14591468
}
14601469
return StringBuilderCache.ReturnAndFree(r);
14611470
}

tests/ServiceStack.OrmLite.Tests/Expression/SqlExpressionTests.cs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,24 @@ public override bool Equals(object obj)
856856
}
857857
}
858858

859+
private class JoinSelectResults2
860+
{
861+
// From TableA
862+
public int Id { get; set; }
863+
public bool Bool { get; set; }
864+
public string Name { get; set; }
865+
866+
// From TableB
867+
public int TableBId { get; set; }
868+
public string TableBName { get; set; }
869+
870+
public override bool Equals(object obj)
871+
{
872+
var other = (JoinSelectResults2)obj;
873+
return Id == other.Id && Bool == other.Bool && Name == other.Name && TableBId == other.TableBId && TableBName == other.TableBName;
874+
}
875+
}
876+
859877
[Test]
860878
public void Can_select_entire_tables()
861879
{
@@ -870,21 +888,38 @@ public void Can_select_entire_tables()
870888
db.Insert(new TableB { Id = 2, TableAId = 2, Name = "NameB2" });
871889
db.Insert(new TableB { Id = 3, TableAId = 2, Name = "NameB3" });
872890

873-
var q = db.From<TableA>()
874-
.Join<TableB>()
875-
.Select<TableA, TableB>((a, b) => new { a, b.TableAId })
876-
.OrderBy(x => x.Id);
877-
878891
try
879892
{
880-
var rows = db.Select<JoinSelectResults1>(q);
881-
var expected = new[]
893+
// Select all columns from TableA
894+
895+
var q1 = db.From<TableA>()
896+
.Join<TableB>()
897+
.Select<TableA, TableB>((a, b) => new { a, b.TableAId })
898+
.OrderBy(x => x.Id);
899+
900+
var rows1 = db.Select<JoinSelectResults1>(q1);
901+
var expected1 = new[]
882902
{
883903
new JoinSelectResults1 { Id = 1, Bool = false, Name = "NameA1", TableAId = 1 },
884904
new JoinSelectResults1 { Id = 2, Bool = true, Name = "NameA2", TableAId = 2 },
885905
new JoinSelectResults1 { Id = 2, Bool = true, Name = "NameA2", TableAId = 2 },
886906
};
887-
Assert.That(rows, Is.EqualTo(expected));
907+
Assert.That(rows1, Is.EqualTo(expected1));
908+
909+
// Same, but use column aliases for some columns from TableB whose names would conflict otherwise
910+
911+
var q2 = db.From<TableA>()
912+
.Join<TableB>()
913+
.Select<TableA, TableB>((a, b) => new { a, TableBId = b.Id, TableBName = b.Name });
914+
915+
var rows2 = db.Select<JoinSelectResults2>(q2).OrderBy(r => r.Id).ThenBy(r => r.TableBId);
916+
var expected2 = new[]
917+
{
918+
new JoinSelectResults2 { Id = 1, Bool = false, Name = "NameA1", TableBId = 1, TableBName = "NameB1" },
919+
new JoinSelectResults2 { Id = 2, Bool = true, Name = "NameA2", TableBId = 2, TableBName = "NameB2" },
920+
new JoinSelectResults2 { Id = 2, Bool = true, Name = "NameA2", TableBId = 3, TableBName = "NameB3" },
921+
};
922+
Assert.That(rows2, Is.EqualTo(expected2));
888923
}
889924
finally
890925
{

0 commit comments

Comments
 (0)