q = (from c in db.CopiasDelLibro
where c.LibroId == idLib &&
!(from a in db.AlquileresDelLibro where a.FechaRealDevolucion.Year == 1900 select a.CopiaDelLibroId).Contains(c.Id)
select c).ToList();
q = (from c in db.CopiasDelLibro
where c.LibroId == idLib &&
!(from a in db.AlquileresDelLibro where a.FechaRealDevolucion.Year == 1900 select a.CopiaDelLibroId).Contains(c.Id)
select c).ToList();
What happens is the following selection:
A) Select all the elements of
CopiasDelLibro
(each one will be calledc
) where of each element (c
) itsLibroId
is equal to idLib .
B) Select the property
CopiaDelLibroId
of all the elements ofAlquileresDelLibro
(each one will be calleda
) where of each element (a
) itsFechaRealDevolucion.Year
is 1990.
C) From the resulting A list, select the
c
elements that, in addition to the first condition, in the list B no you will find theId
(!Contains
) of that elementc
.
Transformed to SQL, it would be more or less like this:
SELECT C.*
FROM CopiasDelLibro C
WHERE C.LibroId = @idLib AND C.Id NOT IN (SELECT A.CopiaDelLibroId
FROM AlquileresDelLibro A
WHERE YEAR(A.FechaRealDevolucion) = 1990)
In Linq, the exclamation symbol refers to the "exclude" method. What your query does is get a list whose property BookId is equal to your varibala idLib and excludes those whose return date is equal to 1900.