Answered by:
Optimizing IEnumerable Linq query

Question
-
User457850011 posted
I have a query which is enumerating over and over and taking very long to run. Can somebody help me optimized it.
public EmpListItem(IEnumerable<EmployeeTable> Emps) { IEnumerable<EmployeeTable> emptable = Emps as EmployeeTable[] ?? Emps.ToArray(); TeamList = emptable.Select(x => x.Surname .Select(c => new EmployeeListItem(c.EmployeeTables.Where(emptable.Contains))) .Where(x => x.EmpDtos.Any()) .ToList(); }
Monday, July 22, 2019 7:10 PM
Answers
-
User457850011 posted
I have figured out my issue. In most of my Linq queries are using custom order by clauses causing performance issues. Also I realized that .any was also slow. Changing it to FirstOrDefault made a big difference. Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 27, 2019 9:11 PM
All replies
-
User475983607 posted
I have a query which is enumerating over and over and taking very long to run. Can somebody help me optimized it.
public EmpListItem(IEnumerable<EmployeeTable> Emps) { IEnumerable<EmployeeTable> emptable = Emps as EmployeeTable[] ?? Emps.ToArray(); TeamList = emptable.Select(x => x.Surname .Select(c => new EmployeeListItem(c.EmployeeTables.Where(emptable.Contains))) .Where(x => x.EmpDtos.Any()) .ToList(); }
Can you explain the expected results of the shared code snippet? It seems you are trying to create a List<EmployeeListItem> where EmpDtos is true.
The IEnumerable<EmployeeTable> input parameter indicates an initial LINQ query was run elsewhere to get the IEnumerable<EmployeeTable> result set. Add the filter to initial LINQ query to reduce the result set.
Monday, July 22, 2019 7:36 PM -
User1520731567 posted
Hi denkyira,
As I can see in your code,I find your business logic is very complicated,
Generally,in this case ,I suggest you could use stored procedure and call it in your project to improve your performance.
Best Regards.
Yuki Tao
Tuesday, July 23, 2019 7:09 AM -
User457850011 posted
I have figured out my issue. In most of my Linq queries are using custom order by clauses causing performance issues. Also I realized that .any was also slow. Changing it to FirstOrDefault made a big difference. Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 27, 2019 9:11 PM