LINQ and nested query

99 Views Asked by At

I need your help, I'm newbie in ASPNET mvc and I have a big problem with LINQ comprehension :D I need to do this simple query:

Select * from Scarico
where Scarico.CaricoId in
(Select Id from Carico where Carico.MattonieraId = ID) <-- ID is

I tried to do this method to retrive the Scarichi List:

private List<Scarico> GetScarichiList(Guid id)
{
    var listaCarichi = from Carico in _db.Carico
                        where Carico.MattonieraId.Equals(id)
                        select Carico.Id;

    var ScarichiList = (from Scarico in _db.Scarico
                            where listaCarichi.Contains(Scarico.Id)
                            select Scarico).ToList();

    return ScarichiList;
}

Second variable "ScarichiList" does not contain any value, actually it should contain 2 values... Where am i wrong?

Thanks in advance

Edit:

Autogenerated SQL: Query 1 (listaCarichi):

SELECT [Extent1].[Id] AS [Id] FROM [dbo].[Carico] AS [Extent1] WHERE [Extent1].[MattonieraId] = @p__linq__0} 

Query 2 (ScarichiList):

SELECT
    [Extent1].[Id]      AS [Id],
    [Extent1].[Stato]   AS [Stato],
    [Extent1].[DataReg] AS [DataReg],
    [Extent1].[UltAgg]  AS [UltAgg],
    ...
FROM [dbo].[Scarico] AS [Extent1]
WHERE EXISTS (SELECT 1 AS [C1] FROM [dbo].[Carico] AS [Extent2] WHERE ([Extent2].[MattonieraId] = @p__linq__0)
  AND ([Extent2].[Id] = [Extent1].[Id]) )
0

There are 0 best solutions below