How could I improve my below LINQ query?
How could I improve my below LINQ query to have a better performance. What is the better way of writing this:
result = (from cd in dataContext.vRetailerWeeks
where cd.MasterTrackingGroupID == masterTrackingGroupId
&& cd.RetailerWeek 开发者_开发技巧<= (from cd1 in dataContext.vCalendarDates
where
cd1.MasterTrackingGroupID == masterTrackingGroupId &&
cd1.CalendarDate == retailerWeekDateTime
select cd1.RetailerWeek).FirstOrDefault()
orderby cd.RetailerWeek descending
select cd.RetailerWeek).Take(weekNo).ToList();
Well, the question is if you need to improve your linq query or your indexes in the database.
The best way it take the SQL that is begin generated (using SQL profiler, or log it from the datacontext).
If you are happy with the SQL generated, there is no need to futher change the linq query, it has done its job. So, try this SQL in Management Studio and examine the query plan. From there you can optimize your indexes.
If you are not happy, go ahead with the linq query.
精彩评论