For SEO, I delete Turkish characters and spaces.
But I can’t format the LINQ parameter,
it gives the error.
The LINQ expression ‘DbSet
.Where(p => p.ProductNameTR
.trCharToEnChar() == __productName_0)’ could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
public List<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null)
{
TContext context = new TContext();
return filter == null ?
context.Set<TEntity>().ToList() :
context.Set<TEntity>().Where(filter).ToList();
}
public static string trCharToEnChar(this string str)
{
var newStr = "";
for (int i = 0; i < str.Length; i++)
{
newStr = str.Replace(" ", "_");
newStr = newStr.Replace("ı", "i");
newStr = newStr.Replace("ö", "o");
newStr = newStr.Replace("ü", "u");
newStr = newStr.Replace("ğ", "g");
newStr = newStr.Replace("ş", "s");
newStr = newStr.Replace("ç", "c");
newStr = newStr.Replace("ü", "u");
}
return newStr;
}
public List<Products> GetProductsByProductNameTextConvert(string productName)
{
return _productDal.GetAll(p => p.ProductNameTR.trCharToEnChar() == productName);
}
2
Answers
Because LINQ does not know how to translate your
trCharToEnChar
method to SQL, you can use.ToList
,AsEnumerable()
or other methods as suggested by error itself to solve it. With.ToList()
after data is loaded, any further operation (such as select) is performed using Linq to Objects, on the data already in memory:The problem occurs because you are using EF Core 3.0 or greater and EF is unable to translate the expression
p => p.ProductNameTR.trCharToEnChar() == productName
to SQL.Prior to EF Core 3.0 the whole set of data would be returned and then further filtered by applying the expression to the result set, returning the filtered result set.
A quick fix would be to change
_productDal.AsEnumerable()
to_productDal.ToList()
to force EF to retrieve the data before filtering.AsEnumerable()
isn’t sufficient to cause EF to execute the query at the database.