Skip to content

Commit

Permalink
Resolve property from the type used in the expression
Browse files Browse the repository at this point in the history
Fixes #282
  • Loading branch information
henkmollema committed Jan 18, 2024
1 parent 3570be7 commit 374c8fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/Dommel/SqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public virtual SqlExpression<TEntity> Select(Expression<Func<TEntity, object>> s
{
if (selector.Body is NewExpression newExpression)
{
props = newExpression.Arguments
.Select(x => (x as MemberExpression)?.Member)
.Where(x => x != null)
.Cast<PropertyInfo>()
props = newExpression
.Arguments
.OfType<MemberExpression>()
.Select(x => x.Expression?.Type.GetProperty(x.Member.Name)!)
.ToArray();
}
}
Expand Down Expand Up @@ -595,12 +595,12 @@ protected virtual object VisitMemberAccess(MemberExpression expression)
protected virtual object VisitConstantExpression(ConstantExpression expression) => expression.Value!;

/// <summary>
/// Proccesses a member expression.
/// Processes a member expression.
/// </summary>
/// <param name="expression">The member expression.</param>
/// <returns>The result of the processing.</returns>
protected virtual string MemberToColumn(MemberExpression expression) =>
Resolvers.Column((PropertyInfo)expression.Member, SqlBuilder);
Resolvers.Column(expression.Expression!.Type.GetProperty(expression.Member.Name)!, SqlBuilder);

/// <summary>
/// Returns the expression operant for the specified expression type.
Expand Down
38 changes: 25 additions & 13 deletions test/Dommel.IntegrationTests/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,13 +36,11 @@ public class Product
public ICollection<ProductOption> Options { get; set; } = new List<ProductOption>();
}

public class Category : IEquatable<Category>
public class Category : NamedEntity, IEquatable<Category>
{
[Key]
public int CategoryId { get; set; }

public string? Name { get; set; }

public bool Equals(Category? other) => CategoryId == other?.CategoryId;
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 374c8fe

Please sign in to comment.