Skip to content

Instantly share code, notes, and snippets.

@zulander1
Last active October 20, 2018 16:17
Show Gist options
  • Save zulander1/6997e43aa9528f2c9abe20931ce11677 to your computer and use it in GitHub Desktop.
Save zulander1/6997e43aa9528f2c9abe20931ce11677 to your computer and use it in GitHub Desktop.
var query = (from o in dbcontext.Orders
let lastStatus = dbcontext.OrderStatus.Where(x => x.OrderId == o.Id).OrderByDescending(x => x.CreatedDate).FirstOrDefault()
where lastStatus.OrderId != 1
select new { o.Name, lastStatus.Id }
).ToList();
@zulander1
Copy link
Author

Output

  SELECT [o].[Name], (
          SELECT TOP(1) [x0].[Id]
          FROM [OrderStatus] AS [x0]
          WHERE [x0].[OrderId] = [o].[Id]
          ORDER BY [x0].[CreatedDate] DESC
      ) AS [Id]
      FROM [Orders] AS [o]
      WHERE (
          SELECT TOP(1) [x].[OrderId]
          FROM [OrderStatus] AS [x]
          WHERE [x].[OrderId] = [o].[Id]
          ORDER BY [x].[CreatedDate] DESC
      ) <> 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment