LINQ表达式无法翻译,当我尝试筛选我的值对象属性时。

huangapple go评论54阅读模式
英文:

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

问题

我在过滤我的查询时遇到了一个问题。这是我的查询:

 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));

这是我的产品实体:

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; }
}

这是我的Name值对象:

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;
        }
    }

这是我映射到数据库的产品实体:

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));
        }
    }

我认为错误是因为在我的实体中使用了'Name'值对象。请帮助我。

英文:

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

 var products = _context.Products.Select(x =&gt; 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 =&gt; 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&lt;ProductImage&gt; ProductImages { get; private set; }
        public virtual ICollection&lt;ProductFeature&gt; 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 &gt; 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&lt;Product&gt;
    {
        public void Configure(EntityTypeBuilder&lt;Product&gt; builder)
        {
            builder.HasKey( x =&gt; x.Id);

            builder.Property(x =&gt; x.Id)
                .ValueGeneratedOnAdd();

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

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

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

答案1

得分: 0

最后我找到了解决方案。我在我的ValueObject类中添加了这行代码,例如在我的Name ValueObject类中,代码如下:

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

然后我修改了我的查询,如下所示:

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

最后,我检查了SQL Server Profiler工具,以确保查询在服务器上执行而不是在客户端上。

英文:

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) =&gt; name.Value;

And I changed my query like below:

var shops = _context.Shops.AsQueryable();            
if (searchKey is not null)
    shops = shops
        .Where(x =&gt; ((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.

huangapple
  • 本文由 发表于 2023年7月20日 20:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定