英文:
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 => 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
答案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) => 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论