user9065477
user9065477

Reputation:

Convert sql Query to LINQ return always null

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?

Upvotes: 0

Views: 89

Answers (1)

JaisG
JaisG

Reputation: 147

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

Upvotes: 1

Related Questions