4
\$\begingroup\$

Is there any way to refactor this?

objLeadTimeReportResult = 
 (from prop in propList
 join book in booking on prop.PropertyId equals book.PropertyId into j
 from book in j.DefaultIfEmpty()
 group book by new { prop.PropertyId, prop.PropertyName } into groupedLeadTime
 select new LeadTimeReportResult
 {
 PropertyId = groupedLeadTime.Key.PropertyId,
 PropertyName = groupedLeadTime.Key.PropertyName,
 Today = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days == 0) ? 1 : 0),
 Days1_2 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 1 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 2) ? 1 : 0),
 Days3_6 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 3 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 6) ? 1 : 0),
 Days7_10 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 7 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 10) ? 1 : 0),
 Days11_15 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 11 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 15) ? 1 : 0),
 Days16_30 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 16 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 30) ? 1 : 0),
 Days31_45 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 31 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 45) ? 1 : 0),
 Days46_60 = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days >= 46 && (item.CheckInDate.Date - item.BookingDate.Date).Days <= 60) ? 1 : 0),
 Days60Plus = groupedLeadTime.Sum(item => ((item.CheckInDate.Date - item.BookingDate.Date).Days > 60) ? 1 : 0)
 }).ToList();

Additional Information

My senior programmer told me to use some instead of count, he claimed that it would make better performance when using on MSSQL Server.

Mathieu Guindon
75.5k18 gold badges194 silver badges467 bronze badges
asked Aug 22, 2011 at 8:35
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

(1). You can create an exntession method to check that item is within the range.

(2). I'd use Count instead of Sum because you just count the number of days in specified interval. It's more natural.

(3). I'd check performance of this query and try to cache result of this join and/or grouping by using some method that requires eager evaluation. ToList() for example. It can help with a lot of traverals of every group in Sum()/Count() methods.

(4). Note for additional information. Even if Sum() in Linq To SQL is a lot faster than Count() you still can have MyCount() implemented as Sum() inside. This will allow you to have a good performance as your senior programmer mentioned and hide this "? 1 : 0" conidition.

 var tempQuery = from prop in propList
 join book in booking on prop.PropertyId equals book.PropertyId into j
 from book in j.DefaultIfEmpty()
 group book by new { prop.PropertyId, prop.PropertyName }.ToList();
 objLeadTimeReportResult = from tempVar in tempQuery
 select new LeadTimeReportResult
 {
 PropertyId = tempVar.Key.PropertyId,
 PropertyName = tempVar.Key.PropertyName,
 Today = tempQuery.Count(item => (item.WithinRange()),
 Days1_2 = tempQuery.Count(item => (item.WithinRange(1,2)),
 Days3_6 = tempQuery.Count(item => (item.WithinRange(3,6)),
 Days7_10 = tempQuery.Count(item => (item.WithinRange(7,10)),
 Days11_15 = tempQuery.Count(item => (item.WithinRange(11,15)),
 Days16_30 = tempQuery.Count(item => (item.WithinRange(16,30)),
 Days31_45 = tempQuery.Count(item => (item.WithinRange(31,45)),
 Days46_60 = tempQuery.Count(item => (item.WithinRange(46,60)),
 Days60Plus = tempQuery.Count(item => (item.GreaterThan(60));
}.ToList();
answered Aug 22, 2011 at 9:01
\$\endgroup\$
3
  • \$\begingroup\$ My senior programmer told me to use some instead of count, he claimed that it would make better performance when using on MSSQL Server. \$\endgroup\$ Commented Aug 22, 2011 at 9:03
  • 2
    \$\begingroup\$ It depends on the implementation of specific LINQ provider and he can be right. I posted my comments with assumption that you use standard LINQ to objects. It would better to add this valuable comment to the first post. \$\endgroup\$ Commented Aug 22, 2011 at 9:06
  • \$\begingroup\$ Actually, i just forgot that SQL don't support using bool so i just need to use tolist, this make my query slower anyway :-P as you said eager evaluation. \$\endgroup\$ Commented Aug 22, 2011 at 10:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.