I am very new using LINQ and I have done few things, right now I have an SQL query that I must implement in LINQ and I have no idea how it is done.
SELECT Boletas.Boleta, DATEPART(DW, CAST(Boletas.Fecha AS datetime)) AS DiaDeLaSemana,
Materiales.Descripcion, Clientes.Nombre,
induscomer.NoCliente AS induscomerNoCliente
FROM Boletas
INNER JOIN Materiales
ON Boletas.Material = Materiales.Material AND Boletas.Familia = Materiales.Familia
INNER JOIN Clientes
ON Boletas.NoCliente = Clientes.NoCliente
LEFT OUTER JOIN induscomer
ON Clientes.NoCliente = induscomer.NoCliente
WHERE (Boletas.Fecha >= '20170101') AND (Boletas.Fecha <= '20170107')
AND (Boletas.Status = 'A')
ORDER BY Boletas.Material
So that if the corresponding client does not exist in the table induscomer , then induscomerNoCliente would be null.
I tried following some aids that I found to do so:
var boletaQuery = (
from b in _context.Boletas
join m in _context.Materiales on b.Material equals m.Material
join c in _context.Clientes on b.NoCliente equals c.NoCliente
join i in _context.induscomer on c.NoCliente equals i.NoCliente into indus
from ind in indus.DefaultIfEmpty()
where b.Fecha.CompareTo(FechaIni) >= 0
&& b.Fecha.CompareTo(FechaFin) <= 0
&& b.Status.Equals('A')
select new {
b,
induscomerNoCliente = ind.NoCliente,
NombreCliente = c.Nombre,
DescripcionMaterial = m.Descripcion
}
);
But when doing boletaQuery.ToList();
it marks me the following error:
Unhandled exception of type 'System.NotSupportedException' in System.Data.Entity.dll Additional information: A constant value of type 'System.Object' could not be created. Only primitive types or enumeration types are supported in this context.