使用Linq Max()获取null异常

时间:2015-12-28 17:22:09

标签: c# entity-framework null

我正在使用EF和一个使用通用查找设计的数据库,该数据库包含两个表(T_LookupCategories和T_LookupValues)和一个SequenceNumber列。 现在我正在尝试编写一个方法,返回特定类别的查找值的最大序列号,但继续获取该例子

The cast to value type 'System.Int32' failed because the materialized value is null.
Either the result type's generic parameter or the query must use a nullable type.

代码是这样的:

    public int GetNextSequenceNumber(T_LookupCategories lookupCategory)
    {
        IList<T_LookupValues> lookupValues = Context.T_LookupValues
            .Where(x => x.ID_LookupCategory == lookupCategory.ID_LookupCategory)
            .ToList();

        return lookupValues == null ? 1 : lookupValues.Max(x => x.SequenceNumber) + 1;
    }

为什么呢?我不明白。 我如何检查空值是否正确?

修改

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace GIE.ITTeamSite.Data.DAL.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class T_LookupCategories
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public T_LookupCategories()
        {
            this.T_LookupValues = new HashSet<T_LookupValues>();
        }

        public int ID_LookupCategory { get; set; }
        public string LookupCategory { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_LookupValues> T_LookupValues { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace GIE.ITTeamSite.Data.DAL.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class T_LookupValues
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public T_LookupValues()
        {
            this.T_Phases = new HashSet<T_Phases>();
            this.T_Phases1 = new HashSet<T_Phases>();
            this.T_Phases2 = new HashSet<T_Phases>();
            this.T_Projects = new HashSet<T_Projects>();
            this.T_Projects1 = new HashSet<T_Projects>();
            this.T_Projects2 = new HashSet<T_Projects>();
            this.T_Projects3 = new HashSet<T_Projects>();
            this.T_Projects4 = new HashSet<T_Projects>();
            this.T_Projects5 = new HashSet<T_Projects>();
            this.T_Projects6 = new HashSet<T_Projects>();
            this.T_Projects7 = new HashSet<T_Projects>();
            this.T_Tasks = new HashSet<T_Tasks>();
            this.T_Tasks1 = new HashSet<T_Tasks>();
            this.T_Tasks2 = new HashSet<T_Tasks>();
        }

        public int ID_LookupValue { get; set; }
        public int ID_LookupCategory { get; set; }
        public string LookupValue { get; set; }
        public int SequenceNumber { get; set; }

        public virtual T_LookupCategories T_LookupCategories { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Phases> T_Phases { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Phases> T_Phases1 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Phases> T_Phases2 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects1 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects2 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects3 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects4 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects5 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects6 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects7 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Tasks> T_Tasks { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Tasks> T_Tasks1 { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Tasks> T_Tasks2 { get; set; }
    }
}

2 个答案:

答案 0 :(得分:1)

你可能应该:

return lookupValues == null ? 1 : (lookupValues.Max(x => (int?)x.SequenceNumber) ?? 0) + 1;

或实现列表

return lookupValues == null ? 1 : 
    lookupValues.Count == 0 ? 1 :
        lookupValues.Max(x => x.SequenceNumber) + 1;

关键是x.SequenceNumberint

答案 1 :(得分:1)

正如大家在评论中所解释的那样,您的列中可能有null值但实体类中没有。

您应该编辑您的类以接受nullable int(int?)或编辑此LINQ:

IList<T_LookupValues> lookupValues = Context.T_LookupValues
    .Where(x => (int?)x.ID_LookupCategory == (int?)lookupCategory.ID_LookupCategory)    //casting to "int?"
    .ToList();