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

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

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

问题

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

  1. var products = _context.Products.Select(x => new GetProductsDto {
  2. Id = x.Id,
  3. Name = x.Name.Value,
  4. Quantity = x.Quantity.Value,
  5. Brand = x.Brand,
  6. Displayed = x.Displayed,
  7. Price = x.Price,
  8. Category = x.Category.Name.Value
  9. });
  10. products = products.Where(p => p.Name.Contains(request.SearchKey));

这是我的产品实体:

  1. public class Product {
  2. public Name Name { get; private set; }
  3. public Quantity Quantity { get; private set; }
  4. public string Brand { get; private set; }
  5. public string Description { get; private set; }
  6. public int Price { get; private set; }
  7. public bool Displayed { get; private set; }
  8. public int CategoryId { get; set; }
  9. public virtual Category Category { get; set; }
  10. public virtual ICollection<ProductImage> ProductImages { get; private set; }
  11. public virtual ICollection<ProductFeature> ProductFeatures { get; private set; }
  12. }

这是我的Name值对象:

  1. public class Name : ValueObject
  2. {
  3. public const byte MaxLen = 100;
  4. public string Value { get; private set; }
  5. private Name()
  6. {
  7. }
  8. public static Name Create(string name)
  9. {
  10. var shopName = new Name();
  11. if (name.Length > MaxLen)
  12. {
  13. shopName.Result.WithError(
  14. string.Format(Validations.MaxLenValidation, DataDictionary.ShopName, MaxLen));
  15. return shopName;
  16. }
  17. shopName.Value = name;
  18. return shopName;
  19. }
  20. }

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

  1. internal class ProductEntityMap : IEntityTypeConfiguration<Product>
  2. {
  3. public void Configure(EntityTypeBuilder<Product> builder)
  4. {
  5. builder.HasKey(x => x.Id);
  6. builder.Property(x => x.Id)
  7. .ValueGeneratedOnAdd();
  8. builder.Property(x => x.Quantity)
  9. .HasConversion(x => x.Value,
  10. x => Domain.Aggregates.Products.ValueObjects.Quantity.Create(x));
  11. builder.Property(x => x.Name)
  12. .HasConversion(x => x.Value,
  13. x => Domain.SharedKernel.Name.Create(x));
  14. }
  15. }

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

英文:

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

  1. var products = _context.Products.Select(x =&gt; new GetProductsDto {
  2. Id = x.Id,
  3. Name = x.Name.Value,
  4. Quantity = x.Quantity.Value,
  5. Brand = x.Brand,
  6. Displayed = x.Displayed,
  7. Price = x.Price,
  8. Category = x.Category.Name.Value
  9. });
  10. products = products.Where(p =&gt; p.Name.Contains(request.SearchKey));

And This is my Product Entity:

  1. public class Product{
  2. public Name Name { get; private set; }
  3. public Quantity Quantity { get; private set; }
  4. public string Brand { get; private set; }
  5. public string Description { get; private set; }
  6. public int Price { get; private set; }
  7. public bool Displayed { get; private set; }
  8. public int CategoryId { get; set; }
  9. public virtual Category Category { get; set; }
  10. public virtual ICollection&lt;ProductImage&gt; ProductImages { get; private set; }
  11. public virtual ICollection&lt;ProductFeature&gt; ProductFeatures { get; private set; }
  12. }

And This My Name ValueObject:

  1. public class Name : ValueObject
  2. {
  3. public const byte MaxLen = 100;
  4. public string Value { get; private set; }
  5. private Name()
  6. {
  7. }
  8. public static Name Create(string name)
  9. {
  10. var shopName = new Name();
  11. if (name.Length &gt; MaxLen)
  12. {
  13. shopName.Result.WithError(
  14. string.Format(Validations.MaxLenValidation, DataDictionary.ShopName, MaxLen));
  15. return shopName;
  16. }
  17. shopName.Value = name;
  18. return shopName;
  19. }
  20. }

And This Is My Product Entity Map to Data Base:

  1. internal class ProductEntityMap : IEntityTypeConfiguration&lt;Product&gt;
  2. {
  3. public void Configure(EntityTypeBuilder&lt;Product&gt; builder)
  4. {
  5. builder.HasKey( x =&gt; x.Id);
  6. builder.Property(x =&gt; x.Id)
  7. .ValueGeneratedOnAdd();
  8. builder.Property(x =&gt; x.Quantity)
  9. .HasConversion(x =&gt; x.Value,
  10. x =&gt; Domain.Aggregates.Products.ValueObjects.Quantity.Create(x));
  11. builder.Property(x =&gt; x.Name)
  12. .HasConversion(x =&gt; x.Value,
  13. x =&gt; Domain.SharedKernel.Name.Create(x));
  14. }
  15. }

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

答案1

得分: 0

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

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

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

  1. var shops = _context.Shops.AsQueryable();
  2. if (searchKey is not null)
  3. shops = shops
  4. .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:

  1. public static explicit operator string(Name name) =&gt; name.Value;

And I changed my query like below:

  1. var shops = _context.Shops.AsQueryable();
  2. if (searchKey is not null)
  3. shops = shops
  4. .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:

确定