Vaccano
Vaccano

Reputation: 82301

Linq To Entity Framework selecting whole tables

I have the following Linq statement:

(from order in Orders.AsEnumerable()
 join component in Components.AsEnumerable()
    on order.ORDER_ID equals component.ORDER_ID
 join detail in Detailss.AsEnumerable()
    on component.RESULT_ID equals detail.RESULT_ID
 where orderRestrict.ORDER_MNEMONIC == "MyOrderText"
 select new 
            {
                Mnemonic = detail.TEST_MNEMONIC,
                OrderID = component.ORDER_ID,
                SeqNumber = component.SEQ_NUM
            }).ToList()

I expect this to put out the following query:

select  *   
from    Orders ord (NoLock)   
        join Component comp (NoLock)
          on ord .ORDER_ID = comp.ORDER_ID
        join Details detail (NoLock)
          on comp.RESULT_TEST_NUM = detail .RESULT_TEST_NUM
where   res.ORDER_MNEMONIC = 'MyOrderText'

but instead I get 3 seperate queries that select all rows from the tables. I am guessing that Linq is then filtering the values because I do get the correct values in the end.

The problem is that it takes WAY WAY too long because it is pulling down all the rows from all three tables.

Any ideas how I can fix that?

Upvotes: 0

Views: 1603

Answers (1)

Andrew Peters
Andrew Peters

Reputation: 11323

Remove the .AsEnumerable()s from the query as these are preventing the entire query being evaluated on the server.

Upvotes: 4

Related Questions