collection具有null的项 - C#和linq

时间:2014-11-21 18:51:21

标签: c# asp.net linq

当代码传递else if时,我收到关于此问题主题的错误(sortColumn ==“DateSubmitted”)

这是我遇到问题的完整代码。

        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();           
    }

非常感谢任何帮助。

谢谢

1 个答案:

答案 0 :(得分:-1)

默认情况下,IQueryables中没有数据,您必须在尝试访问数据之前枚举它。以下是如何做到这一点的。

var results = combined.ToList();