John S
John S

Reputation: 8331

Entity Framework, why is this sql being generated?

When I look at the SQL query generated by EF I see

SELECT [extent1].ID as ID,
[extent1].Name as Name 
From( 
Select myview.ID as ID,
myview.Name as Name
From myview) AS [extent1]
Where([Extent1].ID = @p_linq_0)

Why is the outside select happening on the inside select? I've got a very large table that I can get a record from easily with the outside query but the whole query combined times out.

My Linq query

var result = from i in invitationEntity.Invitations
.Where(a=>a.id == inviationId)
select i;

I am using SQL 2012 & EF5 & Linq.

Is there a way to "force" the simpler query?

Upvotes: 1

Views: 120

Answers (2)

Akash Kava
Akash Kava

Reputation: 39916

Because you are calling "SELECT" once again at the end along with LINQ method.

var result = from i in invitationEntity.Invitations
.Where(a=>a.id == inviationId)
select i;

The last line select i, is useless, but EF is not aware of it whether it has anything useful or not, you can simply avoid it.

var result = invitationEntity.Invitations
.Where(a=>a.id == inviationId);

You can still enumerate result and get everything.

Ok sorry, I forgot to add, you don't have to use "from", you can simply use .Where(expression )

And if you want to use LINQ keywords, then you can use it this way,

var result = from i in invitationEntity.Invitations
where i.id == invitationId
select i;

You cannot mix LINQ keywords and LINQ extension methods.

Upvotes: 5

Fredou
Fredou

Reputation: 20100

i would say that

   var result = from i in invitationEntity.Invitations
               .Where(a=>a.id == inviationId)
                select i;

this

    a=>a.id == inviationId

from a=> generate

  Select myview.ID as ID,
  myview.Name as Name
  From myview

so a is [extent1]

you should use a "standard" where clause

           from i in invitationEntity.Invitations
                  where i.id == inviationId
            select i;

Upvotes: 0

Related Questions