Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a query that is full filling my requirement ,but problem is that when data in that table will increase then it will consume more time ,i want to alternate query in EF so that i will not get that chance. my query is as follow:
C#
var ranks = _service.GetAllEmployeeDuty().OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).Where(x => x.ToSector_Id == sectorId).ToList();
Posted

You say "when data in that table will increase then it will consume more time"

This is not necessarily as bad as you think.
It really depends of the Linq provider.

Let me explain.
When you execute this query, the engine is not going to run it sequentially in this order:
1 - GetAllEmployeeDuty()
2 - OrderByDescending(x => x.EndDate)
3 - GroupBy(x => x.Employee_Id)
4 - Select(x => x.First())
5 - Where(x => x.ToSector_Id == sectorId)
6 - ToList();

On the contrary linq will only build an expression tree, then transform this expression tree into a big SQL request and pass it to the SQL engine.
The SQL speciality is to estimate which path is faster.
The SQL optimization engine will probably start with the smallest tables and can even change its strategy depending on the number of records in each tables.

In summary, again, this is not necessarily as bad as you think.
 
Share this answer
 
v3
I will always suggest, use group by and order by in last
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900