Search code examples
linqentity-framework-coredomain-driven-design

The LINQ expression could not be translated when I try filter my value object property


I encountered an issue when filtering my Query. this is my query:

 var products = _context.Products.Select(x => new GetProductsDto { 
                Id = x.Id,
                Name = x.Name.Value,
                Quantity = x.Quantity.Value,
                Brand = x.Brand,
                Displayed = x.Displayed,
                Price = x.Price,
                Category = x.Category.Name.Value
            });

products = products.Where(p => p.Name.Contains(request.SearchKey));

And This is my Product Entity:

public class Product{
        public Name Name { get; private set; }
        public Quantity Quantity { get; private set; }
        public string Brand { get; private set; }
        public string Description { get; private set; }
        public int Price { get; private set; }
        public bool Displayed { get; private set; }
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
        public virtual ICollection<ProductImage> ProductImages { get; private set; }
        public virtual ICollection<ProductFeature> ProductFeatures { get; private set; }
}

And This My Name ValueObject:

public class Name : ValueObject
    {
        public const byte MaxLen = 100;
        public string Value { get; private set; }

        private Name()
        {
        }

        public static Name Create(string name)
        {
            var shopName = new Name();
            if (name.Length > MaxLen)
            {
                shopName.Result.WithError(
                    string.Format(Validations.MaxLenValidation, DataDictionary.ShopName, MaxLen));
                return shopName;
            }
            shopName.Value = name;
            return shopName;
        }
    }

And This Is My Product Entity Map to Data Base:

internal class ProductEntityMap : IEntityTypeConfiguration<Product>
    {
        public void Configure(EntityTypeBuilder<Product> builder)
        {
            builder.HasKey( x => x.Id);

            builder.Property(x => x.Id)
                .ValueGeneratedOnAdd();

            builder.Property(x => x.Quantity)
                .HasConversion(x => x.Value, 
                    x => Domain.Aggregates.Products.ValueObjects.Quantity.Create(x));

            builder.Property(x => x.Name)
                .HasConversion(x => x.Value,
                    x => Domain.SharedKernel.Name.Create(x));
        }
    }

I think the error is because I'm using 'Name' valueObject In my entity. please help me


Solution

  • Finally I found the solution. I added this line to my ValueObject class, for example in my Name ValueObject class it's like this:

    public static explicit operator string(Name name) => name.Value;
    

    And I changed my query like below:

    var shops = _context.Shops.AsQueryable();            
    if (searchKey is not null)
        shops = shops
            .Where(x => ((string)x.Name).ToLower().Contains(searchKey.ToLower()));
    

    And at the end, I checked the SQL Server Profiler Tools to be sure that query execute in server not in client.