From 374c8fea93a90c54e067d84081ffdf0d7fc6361c Mon Sep 17 00:00:00 2001 From: Henk Mollema Date: Thu, 18 Jan 2024 16:27:56 +0100 Subject: [PATCH] Resolve property from the type used in the expression Fixes #282 --- src/Dommel/SqlExpression.cs | 12 ++++---- test/Dommel.IntegrationTests/Models.cs | 38 +++++++++++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Dommel/SqlExpression.cs b/src/Dommel/SqlExpression.cs index 61cd813..6db0928 100644 --- a/src/Dommel/SqlExpression.cs +++ b/src/Dommel/SqlExpression.cs @@ -73,10 +73,10 @@ public virtual SqlExpression Select(Expression> s { if (selector.Body is NewExpression newExpression) { - props = newExpression.Arguments - .Select(x => (x as MemberExpression)?.Member) - .Where(x => x != null) - .Cast() + props = newExpression + .Arguments + .OfType() + .Select(x => x.Expression?.Type.GetProperty(x.Member.Name)!) .ToArray(); } } @@ -595,12 +595,12 @@ protected virtual object VisitMemberAccess(MemberExpression expression) protected virtual object VisitConstantExpression(ConstantExpression expression) => expression.Value!; /// - /// Proccesses a member expression. + /// Processes a member expression. /// /// The member expression. /// The result of the processing. protected virtual string MemberToColumn(MemberExpression expression) => - Resolvers.Column((PropertyInfo)expression.Member, SqlBuilder); + Resolvers.Column(expression.Expression!.Type.GetProperty(expression.Member.Name)!, SqlBuilder); /// /// Returns the expression operant for the specified expression type. diff --git a/test/Dommel.IntegrationTests/Models.cs b/test/Dommel.IntegrationTests/Models.cs index 98b5ba0..b5f0b45 100644 --- a/test/Dommel.IntegrationTests/Models.cs +++ b/test/Dommel.IntegrationTests/Models.cs @@ -5,15 +5,23 @@ namespace Dommel.IntegrationTests; -public class Product +public abstract class FullNamedEntity +{ + [Column("FullName")] + public string? Name { get; set; } +} + +public abstract class NamedEntity +{ + public string? Name { get; set; } +} + +public class Product : FullNamedEntity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ProductId { get; set; } - [Column("FullName")] - public string? Name { get; set; } - public string? Slug { get; private set; } public void SetSlug(string slug) => Slug = slug; @@ -28,13 +36,11 @@ public class Product public ICollection Options { get; set; } = new List(); } -public class Category : IEquatable +public class Category : NamedEntity, IEquatable { [Key] public int CategoryId { get; set; } - public string? Name { get; set; } - public bool Equals(Category? other) => CategoryId == other?.CategoryId; } @@ -68,18 +74,24 @@ public class OrderLine public string? Line { get; set; } } -public class Foo +public class Foo : NamedEntity { - public int Id { get; set; } + public Foo() + { + Name = nameof(Foo); + } - public string? Name { get; set; } = nameof(Foo); + public int Id { get; set; } } -public class Bar +public class Bar : NamedEntity { - public int Id { get; set; } + public Bar() + { + Name = nameof(Bar); + } - public string? Name { get; set; } = nameof(Bar); + public int Id { get; set; } } public class Baz