✅ System.InvalidOperationException: The LINQ expression
Hello guys. I'm writing a blog API, and in the process of adding a category, I want the first letter of the value in my DTO object to always be capitalized and added to the database in that format. I've tried many attempts, but I keep getting errors, and I haven't been able to find a solution even with AI. If you can help, I'd appreciate it.
CategoryRepo:
public async Task<CategoryDto> CreateCategoryAsync(CategoryDto categoryDto)
{
categoryDto.Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(categoryDto.Name);
var normalizedName = categoryDto.Name.Substring(0, 1).ToUpperInvariant() + categoryDto.Name.Substring(1).ToLowerInvariant();
var existingCategory = await _context.Categories.AnyAsync(x =>
x.Name.ToUpperInvariant() == normalizedName &&
x.Id != categoryDto.Id);
if (existingCategory)
{
throw new InvalidOperationException("Aynı isimde bir kategori zaten var.");
}
var categoryEntity = _mapper.Map<Category>(categoryDto);
await _context.Categories.AddAsync(categoryEntity);
await _unitOfWork.SaveChangesAsync();
return _mapper.Map<CategoryDto>(categoryEntity);
}
Error :
System.InvalidOperationException: The LINQ expression 'DbSet<Category>()\r\n .Any(c => c.Name.Equals(\r\n value: normalizedName_0, \r\n comparisonType: OrdinalIgnoreCase) && c.Id != categoryId_1)' could not be translated. Additional information: Translation of the 'string.Equals' overload with a 'StringComparison' parameter is not supported. See https://go.microsoft.com/fwlink/?linkid=2129535 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
2 Replies
it tells you in the error
Translation of the 'string.Equals' overload with a 'StringComparison' parameter is not supported.
as an aside, you shouldn't be using AddAsync
unless you're using hilo key generationokey