foad abdollahi
foad abdollahi

Reputation: 1978

EF linq: order of where and select in single() query

Which query is optimized :

db.news.Select(c => new { c.Title, c.Date, c.ID })
       .SingleOrDefault(c => c.ID == 1);

or

db.news.Where(c => c.ID == 1)
       .Select(c => new { c.Title, c.Date })
       .SingleOrDefault();

I want this query:

select title, date  
where id = 1

in global which one is better where before select, or where after select?

Upvotes: 1

Views: 962

Answers (1)

Kamal24h
Kamal24h

Reputation: 76

Generally, Where before Select (Where first approach) is more performant, since it filters your result set first, and then executes Select for filtered values only.

Upvotes: -1

Related Questions