1

I am having 2 Lists & I am querying on this 2 List. Both the List are populated with huge data. So the query is taking long time.

When I usually face this performance issue, I simply convert SQL queries & run them directly & get the result in a datatable. But this time I cannot do this as these 2 are not tables but Lists of Models.

How to Optimize this Query or what else should i do?

Code :-

List<TempInOut> listTempInOut = new List<TO_TempInOut>();
List<ShiftSchedule> tempShiftSch  = new List<TO_TempInOut>();

var data = (from B in tempShiftSch
                                    from C in listTempInOut
                                    where
                                        B.CompanyId == companyId &&
                                        C.CompanyId == companyId &&
                                        B.EmployeeId == C.EmployeeId &&
                                        C.InDate >= StrOutStart &&
                                        C.InDate <= StrOutEnd &&
                                        B.ShiftId == item.ShiftCode &&
                                        B.ShiftDate == tempInputDate
                                    select new
                                    {
                                        C.EmployeeId,
                                        C.InDate,
                                        C.Time_Date1
                                    }).ToList();
Anup
  • 9,396
  • 16
  • 74
  • 138

3 Answers3

0

Implement an IEqualityComparer for your types, use HashSet for each collection, and use the HashSet.Intersect method to get your output.

0

You can simplify your query to two stepsand compare time. I am thinking of something like that.

var listTempInOutResult = listTempInOut.Where(C => C.CompanyId == companyId 
                                               && C.InDate >= StrOutStart 
                                               && C.InDate <= StrOutEnd);
var employessIds = listTempInOutresult.Select(x => x.EmployeeId).ToList();

var data = tempShiftSch.Where(B => employessIds.Contains(B.EmployeeId) 
                               && B.CompanyId == companyId 
                               && B.ShiftDate == tempInputDate 
                               && B.ShiftId == item.ShiftCode)
                        .Select(C=> new
                        {
                          C.EmployeeId,
                          C.InDate,
                          C.Time_Date1
                        }).ToList();
Ahmed
  • 1,542
  • 2
  • 13
  • 21
0

if you are working with iqueryable it would be better to use joins. see this StackOverflow question

Community
  • 1
  • 1