Someone can explain this LINQ of C # (more than all the "!" sign and contains) and convert it to a SQL Server Query [closed]

0
            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();
    
asked by Josiel Castillo 31.07.2018 в 05:15
source

2 answers

1

What happens is the following selection:

  

A) Select all the elements of CopiasDelLibro (each one will be called c ) where of each element ( c ) its LibroId is equal to idLib .

  

B) Select the property CopiaDelLibroId of all the elements of AlquileresDelLibro (each one will be called a ) where of each element ( a ) its FechaRealDevolucion.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 the Id ( !Contains ) of that element c .

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)
    
answered by 31.07.2018 / 19:39
source
0

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.

    
answered by 31.07.2018 в 06:12