2014-11-21 75 views
-1

當代碼經過否則我得到關於這個問題的主題的錯誤,如果(sortColumn ==「DateSubmitted」)收集與空項目 - C#和LINQ

這是我遇到的整個代碼問題。

 private object GetContacts(int Month, int Year, string sortColumn = "FacilityName", bool asc = true, bool byRegion = true, bool myContacts = false) 
    { 
     var userID = GetCurrentUserID(); 

     sharedmsdbEntity db = new sharedmsdbEntity(); 

     var user = (from e in db.smsEmployees where e.userID == userID select e).FirstOrDefault(); 

     //displaySelector.Visible = user.userJobTitle == "RM"; 

     IQueryable<smsCustomerContact> starter; 

     var emptyCustomers = (from c in db.smsCustomers 
           where 
           !myContacts 
           && c.custActive 
           && !(from cc in c.smsCustomerContacts where !cc.isDeleted && cc.cntActive && (cc.cntIsMRI || cc.cntIsPETCT || cc.cntIsCT || cc.cntIsFFDM) select cc).Any() 
           select c); 

     if ((user.userAdmin.HasValue && user.userAdmin.Value) || (user.userRegionID.HasValue && user.smsRegion.Name == "Corporate")) 
     { 
      starter = (from c in db.smsCustomerContacts where c.smsCustomer != null && !c.isDeleted && c.cntActive && ((c.cntIsMRI && c.smsCustomer.IsMRIBillingActive) || (c.cntIsPETCT && c.smsCustomer.IsPETBillingActive) || (c.cntIsCT && c.smsCustomer.ISCTBillingActive) || (c.cntIsFFDM && c.smsCustomer.ISFFDMBillingActive)) select c); 
     } 
     else 
     { 
      if (byRegion) 
      { 
       starter = (from c in db.smsCustomerContacts where c.smsCustomer != null && !c.isDeleted && c.cntActive && ((c.cntIsMRI && c.smsCustomer.IsMRIBillingActive) || (c.cntIsPETCT && c.smsCustomer.IsPETBillingActive) || (c.cntIsCT && c.smsCustomer.ISCTBillingActive) || (c.cntIsFFDM && c.smsCustomer.ISFFDMBillingActive)) && user.userRegionID.HasValue && c.smsCustomer.custRegionID.HasValue && c.smsCustomer.custRegionID == user.userRegionID select c); 
       emptyCustomers = (from c in emptyCustomers where c.custRegionID.HasValue && user.userRegionID.HasValue && c.custRegionID == user.userRegionID select c); 
      } 
      else 
      { 
       starter = (from c in db.smsCustomerContacts where c.smsCustomer != null && !c.isDeleted && c.cntActive && ((c.cntIsMRI && c.smsCustomer.IsMRIBillingActive) || (c.cntIsPETCT && c.smsCustomer.IsPETBillingActive) || (c.cntIsCT && c.smsCustomer.ISCTBillingActive) || (c.cntIsFFDM && c.smsCustomer.ISFFDMBillingActive)) && c.cntCreatedBy == userID select c); 
      } 
     } 
     if (myContacts) 
     { 
      starter = (from c in starter where (from m in c.smsEmployeeContacts where m.empID == userID select m).Any() select c); 
     } 

     if ((from c in starter where (from s in c.smsContactFormSents where s.completed orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault().dateAccepted.Value.Year < Year select c).Any()) 
     { 
      btnPreviousMonth.Style.Remove("display"); 
     } 
     else 
     { 
      btnPreviousMonth.Style.Add("display", "none"); 
     } 

     bool isCurrentYear = SiteFunctions.Now().Year == Year; 

     starter = (from c in starter 
        where 
        (c.cntActive 
        || ((from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault().dateAccepted.HasValue && (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault().dateAccepted.Value.Year == Year)) 
        && ((isCurrentYear && c.smsCustomer.custActive 
         && ((c.smsCustomer.custType != 2 && c.cntIsPETCT && c.smsCustomer.IsPETBillingActive) || ((c.cntIsMRI && c.smsCustomer.IsMRIBillingActive) || (c.cntIsCT && c.smsCustomer.ISCTBillingActive) || (c.cntIsFFDM && c.smsCustomer.ISFFDMBillingActive)))) //Only PET/CT is not allowed with Retail 
        || (((from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault().dateAccepted.HasValue && (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault().dateAccepted.Value.Year == Year) 
         || (from s in c.smsContactFormSents orderby s.sentDateTime descending select s).FirstOrDefault().sentDateTime.Year == Year) 
         && c.smsCustomer.custActive 
         && ((c.smsCustomer.custType != 2 && c.cntIsPETCT && c.smsCustomer.IsPETBillingActive) || ((c.cntIsMRI && c.smsCustomer.IsMRIBillingActive) || (c.cntIsCT && c.smsCustomer.ISCTBillingActive) || (c.cntIsFFDM && c.smsCustomer.ISFFDMBillingActive)))) //Only PET/CT is not allowed with Retail 
        select c); 

     var data = (from c in starter 
        select new ACPListObject() 
        { 
         contact = c, 
         Year = Year, 
         LastSentDate = (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.sentDateTime).FirstOrDefault(), 
         LastSubmit = (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm).FirstOrDefault(), 
         lastFormComplete = (from s in c.smsContactFormSents orderby s.sentDateTime descending select s).FirstOrDefault(), 
         isMyContact = (from m in c.smsEmployeeContacts where m.empID == userID select m).Any() 
        }); 

     if (isCurrentYear) 
     { 
      var custs = (from c in emptyCustomers.ToList() 
         where c.ISCTBillingActive || c.ISFFDMBillingActive || c.IsMRIBillingActive || (c.IsPETBillingActive && c.custType != 2) 
         select new ACPListObject() 
         { 
          contact = new smsCustomerContact() { cntID = -1, smsCustomer = c, cntActive = true }, 
          Year = Year, 
          LastSentDate = new DateTime(), 
          LastSubmit = (ACPForm)null, 
          lastFormComplete = (smsContactFormSent)null, 
          isMyContact = false 
         }); 

      var combined = data.ToList(); 

      combined.AddRange(custs); 

      data = combined.AsQueryable();     

     } 

     if (sortColumn == "Contact") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.contact.cntFirstName + " " + o.contact.cntLastName);      
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.contact.cntFirstName + " " + o.contact.cntLastName); 
      } 
     } 
     else if (sortColumn == "City") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.contact.smsCustomer.custCity); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.contact.smsCustomer.custCity); 
      } 
     } 
     else if (sortColumn == "State") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.contact.smsCustomer.custState); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.contact.smsCustomer.custState); 
      } 
     } 
     else if (sortColumn == "DateSent") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.LastSentDate); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.LastSentDate); 
      } 
     } 
     else if (sortColumn == "DateSubmitted") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.lastFormComplete.ACPForm.dateAccepted); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.lastFormComplete.ACPForm.dateAccepted);      
      } 
     } 
     else if (sortColumn == "FacilityName") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.contact.smsCustomer.custFacilityName); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.contact.smsCustomer.custFacilityName); 
      } 
     } 
     else if (sortColumn == "BillingCode") 
     { 
      if (asc) 
      { 
       return data.ToList().OrderBy(o => SiteFunctions.GetBillingCodes(o.contact, false)); 
      } 
      else 
      { 
       return data.ToList().OrderByDescending(o => SiteFunctions.GetBillingCodes(o.contact, false)); 
      } 
     } 
     else if (sortColumn == "MyContact") 
     { 
      if (asc) 
      { 
       data = data.OrderBy(o => o.isMyContact); 
      } 
      else 
      { 
       data = data.OrderByDescending(o => o.isMyContact); 
      } 
     } 

     return data.ToList();   
    } 

任何幫助,慷慨讚賞。

謝謝默認

+0

'lastFormComplete'似乎爲空。你如何填充它? – 2014-11-21 18:53:12

+0

'data'的類型是什麼? – BradleyDotNET 2014-11-21 18:53:36

+0

Acutally Jon Skeet在該頁面上沒有任何內容顯示您無法以所示方式分析IQuerabyable的內容。 – 2014-11-21 19:03:59

回答

-1

IQueryables其中沒有任何數據,你必須枚舉它試圖訪問數據之前。這是你如何做到的。

var results = combined.ToList(); 
+0

這也會給出不同的例外。 – BradleyDotNET 2014-11-21 18:56:12

+0

這不是說'Count'是零,而是集合的項目具有null'lastFormComplete'屬性。 – 2014-11-21 18:56:58

+0

好吧足夠更新。碼。 – 2014-11-21 18:58:31