0

I would like to know if someone has experienced the same thing as me and how they could solve it.

My problem is the following: I have this class:

public class FacturacionValueObject
    {
        public MovStock MovStockId { get; private set; }
        public CuentaCorriente CuentaCorrienteId { get; private set; }
        public Numero NumeroNpca { get; private set; }
        public Tipo TipoNpca { get; private set; }
        public Fecha FechaEmision { get; private set; }
    }

this class in turn is composed of classes that inherit from an abstract class valueobject For example:

public sealed class Numero : ValueObject
    {
        public decimal NumeroId { get; private set; }

        public Numero(decimal numeroId)
        {
            NumeroId = numeroId;
        }

        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return NumeroId;
        }
    }

I use repository and unit of work and I have the following generic method:

public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
        {
            try
            {
                return await _context.Set<T>().Where(predicate).AsNoTracking().ToListAsync();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

when using it I do it as follows:

Expression<Func<FacturacionValueObject, bool>> expression = f => f.NumeroNpca.NumeroId == 441268;

        var datos = await _unitOfWork.Repository<FacturacionValueObject>().GetAsync(predicate: expression);

when I execute it in my API, the catch returns the following exception:

The LINQ expression 'DbSet() .Where(f => f.NumeroNpca.NumeroId == 441268)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

I have searched on the internet but nothing has worked for me so far. I would really appreciate if someone can help me with this. thank you so much.

At first I thought it could be the Billing Value Object class since it's the first time I've used linq.expressions but I don't know yet because it's the first time I've used linq.expressions

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • what happens when call it bare bone ? var x = _context.'DbSet'.Where(f => f.NumeroNpca.NumeroId == 441268); – jeb Mar 31 '23 at 15:46
  • I just tried but with the same result: System.InvalidOperationException: 'The LINQ expression 'DbSet() .Where(f => f.NumeroNpca.NumeroId == 441268)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.' – Alex Magatama Mar 31 '23 at 15:57
  • Value objects are not very well supported in EF Core yet (I think they are working on for the next release). Meanwhile, it depends how you have mapped (configured) the EF model for that property - e.g. owned entity type, value converter etc. So in order to get concrete answer, you need to provide [mre] - at minimum sample entity class using such property, and model configuration (fluent API). – Ivan Stoev Mar 31 '23 at 17:04

0 Answers0