Base solution for your next web application
Open Closed

LINQ expression could not be translated #8582


User avatar
0
alfar_re created

Previous title: LOOK UP TABLE ISSUE

Hi,

Am getting this error on the filter on look up tables.

System.InvalidOperationException: The LINQ expression 'DbSet<RentalInvoice> .Where(r => __ef_filter__p_0 || !(((ISoftDelete)r).IsDeleted) && __ef_filter__p_1 || (Nullable<int>)((IMustHaveTenant)r).TenantId == __ef_filter__CurrentTenantId_2) .Where(r => r.InvoiceNumber.ToString().Contains(__input_Filter_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. at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& ) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Please share your query code.

    eg

    .Where(r => r.InvoiceNumber.ToString().Contains(__input_Filter_0)
    
  • User Avatar
    0
    alfar_re created

    Here you go. That's the entire code block.

    var query = _lookup_rentalInvoiceRepository.GetAll().WhereIf(
    		!string.IsNullOrWhiteSpace(input.Filter),
    	   e=> e.InvoiceNumber.ToString().Contains(input.Filter)
    	);
    
    var totalCount = await query.CountAsync();
    
    var rentalInvoiceList = await query
    	.PageBy(input)
    	.ToListAsync();
    
    var lookupTableDtoList = new List<TransactionRentalInvoiceLookupTableDto>();
    foreach(var rentalInvoice in rentalInvoiceList){
    	lookupTableDtoList.Add(new TransactionRentalInvoiceLookupTableDto
    	{
    		Id = rentalInvoice.Id,
    		DisplayName = string.Format(rentalInvoice.InvoiceNumber?.ToString(), " - ", rentalInvoice.Description, "(", rentalInvoice.Status, ")")
    	});
    }
    
    return new PagedResultDto<TransactionRentalInvoiceLookupTableDto>(
    	totalCount,
    	lookupTableDtoList
    );
    
  • User Avatar
    0
    maliming created
    Support Team

    hi It looks like InvoiceNumber is not a string and ef core cannot translate the ToString method.

  • User Avatar
    0
    alfar_re created

    Hi,

    InvoiceNumber is actually a string. And the same issue is occuring in almost all look up tables.

  • User Avatar
    0
    maliming created
    Support Team
    • What is your product version?
    • What is your abp framework version?

    InvoiceNumber is actually a string

    Did you try removing the ToString() method?

    var query = _lookup_rentalInvoiceRepository.GetAll().WhereIf(
    		!string.IsNullOrWhiteSpace(input.Filter),
    	   e=> e.InvoiceNumber.Contains(input.Filter)
    	);
    
  • User Avatar
    0
    alfar_re created

    Hi,

    I actually changed all filter methods and removed the ToString()where the property was a string and it worked well.

    Allow me to close this now. Many thanks for the help. Cheers!