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

Commit 783858b

Browse files
authored
Fix select filed alias conflict bug of main table (#653)
1 parent 7c45723 commit 783858b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,8 @@ private object SetAnonTypePropertyNamesForSelectExpression(object expr, Expressi
21402140
{
21412141
if (UseSelectPropertiesAsAliases || // Use anon property alias when explicitly requested
21422142
propExpr.Member.Name != member.Name || // or when names don't match
2143-
propExpr.Expression.Type != ModelDef.ModelType) // or when selecting a field from a different table
2143+
propExpr.Expression.Type != ModelDef.ModelType || // or when selecting a field from a different table
2144+
member.Name != ModelDef.FieldDefinitions.First(x => x.Name == member.Name).FieldName) //or when name and alias don't match
21442145
return new SelectItemExpression(DialectProvider, expr.ToString(), member.Name);
21452146

21462147
return expr;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ public void Can_Select_Single_from_just_FROM_Expression()
291291
Assert.That(result, Is.Not.Null);
292292
}
293293
}
294+
295+
296+
[Test]
297+
public void Can_Select_Filed_Alias_Expression()
298+
{
299+
300+
using (var db = OpenDbConnection())
301+
{
302+
var sql = db.From<Employee>()
303+
.Join<Company>((e, c) => e.CompanyId == c.Id)
304+
.Select<Employee, Company>((e, c) =>
305+
new
306+
{
307+
Id = e.Id,
308+
Name = e.Name, // test this property use alias
309+
CompanyName = c.Name
310+
});
311+
Assert.AreEqual(sql.SelectExpression, "SELECT \"Employee\".\"Id\", \"Employee\".\"EmployeeName\" AS \"Name\", \"Company\".\"CompanyName\" AS \"CompanyName\"");
312+
}
313+
314+
}
294315
}
295316

296317
public class Submission
@@ -301,4 +322,19 @@ public class Submission
301322
public string Body { get; set; }
302323
}
303324

325+
public class Company
326+
{
327+
public int Id { get; set; }
328+
[Alias("CompanyName")]
329+
public string Name { get; set; }
330+
}
331+
332+
public class Employee
333+
{
334+
public int Id { get; set; }
335+
public int CompanyId { get; set; }
336+
[Alias("EmployeeName")]
337+
public string Name { get; set; }
338+
}
339+
304340
}

0 commit comments

Comments
 (0)