Estoy tratando de escribir una consulta LINQ para obtener un resultado de consulta solo si un valor está dentro de un valor de lista predefinido.

Pero obteniendo la siguiente excepción

The LINQ expression 'Where<TransparentIdentifier<TransparentIdentifier<EmployeeBenefit, EmployeeEntity>, EmployerEntity>>(
source: Join<TransparentIdentifier<EmployeeBenefit, EmployeeEntity>, EmployerEntity, int, TransparentIdentifier<TransparentIdentifier<EmployeeBenefit, EmployeeEntity>, EmployerEntity>>(
    outer: Join<EmployeeBenefit, EmployeeEntity, Nullable<int>, TransparentIdentifier<EmployeeBenefit, EmployeeEntity>>(
        outer: DbSet<EmployeeBenefit>, 
        inner: DbSet<EmployeeEntity>, 
        outerKeySelector: (e) => e.EmployeeEntityCntr, 
        innerKeySelector: (e0) => (Nullable<int>)e0.EmployeeEntityCntr, 
        resultSelector: (e, e0) => new TransparentIdentifier<EmployeeBenefit, EmployeeEntity>(
            Outer = e, 
            Inner = e0
        )), 
    inner: DbSet<EmployerEntity>, 
    outerKeySelector: (ti) => ti.Inner.EmployerEntityCntr, 
    innerKeySelector: (e1) => e1.EmployerEntityCntr, 
    resultSelector: (ti, e1) => new TransparentIdentifier<TransparentIdentifier<EmployeeBenefit, EmployeeEntity>, EmployerEntity>(
        Outer = ti, 
        Inner = e1
    )), 
predicate: (ti0) => ti0.Outer.Outer.EmployeeCntr == (Unhandled parameter: __iEmployeeID_0) && !(ti0.Outer.Outer.EndDate.HasValue) || ti0.Outer.Outer.EndDate >= (Nullable<DateTime>)DateTime.Now && ti0.Outer.Outer.StartDate <= DateTime.Now && ti0.Outer.Outer.CarRego == (Unhandled parameter: __registration_1) && Any<Benefit>(
    source: (Unhandled parameter: ___benefitCodes_2), 
    predicate: (b) => b.Code == ti0.Outer.Outer.BenefitCode))' 

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.

Si elimino la siguiente condición de la consulta LINQ, la consulta funciona sin errores

&& _benefitCodes.Any (b => b.Code == employeeBenefit.BenefitCode)

¿Podrías decirme qué me estoy perdiendo aquí?

Consulta LINQ:

 _benefitCodes = new List<Benefit>(){
                new Benefit() { Name = "Maitenance", Code = "AAAAA" },
                new Benefit() { Name = "Maitenance", Code = "BBBBB" }
            };

currentBudgets = (from employeeBenefit in _context.EmployeeBenefit
                                  join employeeEntity in _context.EmployeeEntity
                                  on employeeBenefit.EmployeeEntityCntr equals employeeEntity.EmployeeEntityCntr
                                  join employerEntity in _context.EmployerEntity
                                  on employeeEntity.EmployerEntityCntr equals employerEntity.EmployerEntityCntr
                                  where employeeBenefit.EmployeeCntr == iEmployeeID
                                  && (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
                                  && employeeBenefit.StartDate <= DateTime.Now
                                  && employeeBenefit.CarRego == registration
                                  && _benefitCodes.Any(b => b.Code == employeeBenefit.BenefitCode)
                                  select new BenefitCurrentBudget
                                  {
                                      Cntr = employeeBenefit.Cntr,
                                      BenefitCode = employeeBenefit.BenefitCode,
                                      PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
                                      InstalmentAmount = employeeBenefit.InstalmentAmt,
                                      TotalAmount = employeeBenefit.TotalAmt,
                                      CarRego = employeeBenefit.CarRego,
                                      ApprovedFlag = employeeBenefit.ApprovedFlag
                                  }).ToList();

I need values from the query result where the result has employeeBenefit.BenefitCodes only from the pre-defined list _benefitCodes.Code
0
Aryan M 16 oct. 2019 a las 04:19

1 respuesta

La mejor respuesta

Si la única causa del error es el _benefitCodes.Any(b => b.Code == employeeBenefit.BenefitCode), intente esto

var onlyCodes = _benefitCodes.Select(x => x.Code).ToList();

Y cambia tu _benefitCodes.Any(b => b.Code == employeeBenefit.BenefitCode) a

&& onlyCodes.Contains(employeeBenefit.BenefitCode)

Qué error Linq le está diciendo que no puede convertir la construcción de su consulta y si está seguro de que si eliminó _benefitCodes.Any(b => b.Code == employeeBenefit.BenefitCode) está funcionando, entonces no puede traducir esa consulta.

1
Gabriel Llorico 16 oct. 2019 a las 01:37