[EDITED: I left the original question below, with some more context and code to reproduce the problem. The short version below contains the essence of the question]
Shor
hi this can be made to work quite easy...
Instead of an IQueryable
Dim orders As IQueryable(Of Order) = db.Orders
use a List
Dim orders as List(of Order) = db.Orders.ToList()
Does this work?
Dim a = From p In products Join o In orders On p.Id Equals o.ProductId
Order By p.DateCreated
Select New With {.id = p.Id, .OrderDate = o.OrderDate, .CustomerId = o.CustomerId, .DateCreated = p.DateCreated}
Dim b = From outerOrder In orders _
Where outerOrder.OrderDate = currentDate _
AndAlso outerOrder.ProductId = _
(From x In a Where x.OrderDate = outerOrder.OrderDate _
AndAlso x.CustomerId = outerOrder.CustomerId _
Select x.id).FirstOrDefault
Moving the Order By p.DateCreated
line as per my edited answer allows the query to run without any exceptions. However, the emitted SQL is different so I don't think you are getting back the correct result.
Dim qLinq = From outerOrder In orders
Let id = (From p In products
Order By p.DateCreated
Join o In orders On p.Id Equals o.ProductId
Where o.OrderDate = outerOrder.OrderDate AndAlso
outerOrder.CustomerId = o.CustomerId
Select p.Id).FirstOrDefault()
Where outerOrder.OrderDate = currentDate AndAlso
outerOrder.ProductId = id
Select outerOrder
I think the following works, the SQL is ugly though:
Dim qLinq = From outerOrder In orders
Where outerOrder.OrderDate = currentDate AndAlso
outerOrder.ProductId =
(From x In (From p In products
Join o In orders On p.Id Equals o.ProductId
Where o.OrderDate = outerOrder.OrderDate AndAlso
outerOrder.CustomerId = o.CustomerId
Select p.Id)
Order By x).FirstOrDefault()
Select outerOrder