0

I have this sql Query:

SELECT *
  FROM [Incentivos].[dbo].[Incentivos] AS I 
  INNER JOIN [Incentivos].[dbo].[Captura] AS C ON I.CapturaID = C.ID
  WHERE I.EmpleadoID =4530 and C.mdEstatusRegistro = 1

It works correctly, now I want to use it with LINQ, so I try

  var ce = _contexto.Empleados.Where(x => x.nCodigoEmpleado == codigoEmpleado)
           .Select(x => x.ID).FirstOrDefault(); //4530 value

 var captura = _contexto.Capturas.Where(x => x.mdEstatusRegistro).ToList(); //Captura table

  //JOIN
  var Incentivo = _contexto.Incentivos 
  .Join(captura, x => x.CapturaID, y => y.ID, (x, y) => new { x, y })
  .Where(x => x.y.mdEstatusRegistro && x.x.EmpleadoID == ce)
  .Select(b=> b.x).FirstOrDefault();

Problem is Join always come null, what am I doing wrong? regards

UPDATE: I change query more simple like

 var Incentivo = from incentivos in _contexto.Incentivos
                 join captura in _contexto.Capturas on incentivos.CapturaID equals captura.ID
                 where (incentivos.EmpleadoID == ce) && (captura.mdEstatusRegistro == true)
                 select incentivos;

But now after query I want to access Incentivo like:

Incentivo.nPorcentajeAJU //Property of Incentivo table

But I can't , how can I access variables now?

14
  • I can't get more than one value because this AND C.mdEstatusRegistro = 1 validation in sql just return one value @DavidG Commented Feb 22, 2018 at 16:41
  • try to replace x.nCodigoEmpleado == (int)codigoEmpleado with SqlFunctions.StringConvert(x.nCodigoEmpleado)==codigoEmpleado Commented Feb 22, 2018 at 16:41
  • 1
    @DavidG No it won't. FirstOrDefault only returns the default value for an empty set, otherwise it returns the first item. Commented Feb 22, 2018 at 16:41
  • But it should get a value. like sql instruction, it's supposed I'm reply it but with LINQ @juharr Commented Feb 22, 2018 at 16:43
  • 1
    @Pepe you shouldn't have to write anything more than contexto.Empleados.Where(emp=>emp.Captura.mdEstatusRegistro && emp.EmpleadoID==4530) Commented Feb 22, 2018 at 16:52

1 Answer 1

1

Your LINQ query will always return a List<Incentivos>, so the only way you can access Incentivo.nPorcentajeAJU is in a foreach loop. However as you state query only returns one record then you must write like this:

var Incentivo = (
  from incentivos in _contexto.Incentivos
  join captura in _contexto.Capturas on incentivos.CapturaID equals captura.ID
  where (incentivos.EmpleadoID == ce) && (captura.mdEstatusRegistro == true)
  select incentivos
).First<Incentivos>();

int someVar = Incentivo.Some_Property; //Access the property like so
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.