从类中尝试OrderBy日期属性时,IQueryable设置为null

时间:2014-11-22 00:52:10

标签: asp.net linq

我是新来的链接,这是我使用它的第一个任务,并且进展不顺利。 我有一个页面可以按照所点击的列进行排序。 其中一列是Date列,DateSubmitted。

我收到的对象错误未设置为对象的实例...

我逐步完成了代码,注意到list对象只包含值,直到它通过了orderby行代码。我认为这是因为dateAccepted属性包含空值,就在assignemnt data = data.OrderBy ...之前,数据保存416条记录,在通过赋值后,它有null,并抛出错误。

我有另外一个类似于这个的问题,我为重复道歉,但我真的需要一些解释,如果可能的话,以及如何解决这个问题的一些帮助。

我尝试在dateAccepted之后添加HasValue,但这没有什么区别。

任何帮助都很有价值。非常感谢你。

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

    }
        else if (sortColumn == "DateSubmitted")
    {
        if (asc)
        {   
          data = data.OrderBy(o => o.lastFormComplete.ACPForm.dateAccepted);
        }
        else
        {
            data = data.OrderByDescending(o => o.lastFormComplete.ACPForm.dateAccepted);                    
        }
    }


    return data.ToList();           
}

EDIT 我尝试了你提出的解决方案,我认为它正在工作,但由于某种原因,dateAccepted即“dateSubmitted”的值没有被填充。

他们的方式是在代码中连接对象以获得dateAccepted是这样的:

ACPListObject

        public class ACPListObject
{
    public smsCustomerContact contact { get; set; }
    public int Year { get; set; }
    public DateTime LastSentDate { get; set; }
    public ACPForm LastSubmit { get; set; }
    public DateTime? dateAccepted { get; set; }
    public smsContactFormSent lastFormComplete { get; set; }
    public bool isMyContact { get; set; }
}

smsContactFormSent

    public partial class smsContactFormSent
{
    public System.Guid ID { get; set; }
    public int cntID { get; set; }
    public int sentBy { get; set; }
    public System.DateTime sentDateTime { get; set; }
    public bool completed { get; set; }
    public Nullable<int> acpID { get; set; }

    public virtual ACPForm ACPForm { get; set; }
    public virtual smsCustomerContact smsCustomerContact { get; set; }
    public virtual smsEmployee smsEmployee { get; set; }
}

public partial class ACPForm
{
    public ACPForm()
    {
        this.ACPFormContacts = new HashSet<ACPFormContact>();
        this.smsContactFormSents = new HashSet<smsContactFormSent>();
    }

    public int acpID { get; set; }
    public int romID { get; set; }
    public int customerID { get; set; }
    public Nullable<System.DateTime> dateSent { get; set; }
    public Nullable<System.DateTime> dateAccepted { get; set; }

}

可能是因为我没有正确访问smsContactFormSent而没有获取值吗?

这就是我实施解决方案的方式:

    public class ACPListObject
{
    public smsCustomerContact contact { get; set; }
    public int Year { get; set; }
    public DateTime LastSentDate { get; set; }
    public ACPForm LastSubmit { get; set; }
    public DateTime? dateAccepted { get; set; }
    public smsContactFormSent lastFormComplete { get; set; }
    public bool isMyContact { get; set; }
}

            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(),
                        dateAccepted = (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm.dateAccepted).FirstOrDefault(), //note that I am selecting dateAccept here so, this will be a DateTime or null                          
                        isMyContact = (from m in c.smsEmployeeContacts where m.empID == userID select m).Any()
                    });

            else if (sortColumn == "DateSubmitted")
        {
            if (asc)
            {   
              //data = data.OrderBy(o => o.lastFormComplete.ACPForm.dateAccepted);                  
              data = data.OrderBy(o => o.dateAccepted);//order by new variable
            }
            else
            {
                //data = data.OrderByDescending(o => o.lastFormComplete.ACPForm.dateAccepted);                    
                data = data.OrderByDescending(o => o.dateAccepted); //order by new variable
            }
        }

发送新的ContactForm时,不会填充dateSubmitted的值。我保持diggin,看看错误是否来自其他地方。非常感谢你对Marcos的帮助。

1 个答案:

答案 0 :(得分:1)

检查此解决方案并适应您的模型。

 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(),
                        dateAccepted  = (from s in c.smsContactFormSents orderby s.sentDateTime descending select s.ACPForm.dateAccepted).FirstOrDefault(), //note that I am selecting dateAccept here so, this will be a DateTime or null
                        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();                

        }
        else if (sortColumn == "DateSubmitted")
        {
            if (asc)
            {   
                data = data.OrderBy(o => o.dateAccepted);//order by new variable
            }
            else
            {
                data = data.OrderByDescending(o => o.dateAccepted); //order by new variable
            }
        }


        return data.ToList();  

编辑: 如果可能,您应该将dateAccepted添加到ACPListObject类:

public class ACPListObject
{
    public smsCustomerContact contact { get; set; }
    public int Year { get; set; }
    public DateTime LastSentDate { get; set; }
    public ACPForm LastSubmit { get; set; }
    public smsContactFormSent lastFormComplete { get; set; }
    public bool isMyContact { get; set; }
    public DateTime? dateAccepted { get; set; }
}

如果您无法添加此属性,则此解决方案无效。如果这对您有用,请告诉我。