LINQ:System.Int32是一个不可为空的值类型

时间:2011-01-24 23:54:59

标签: c# .net linq linq-to-sql

错误:无法将null值分配给System.Int32类型的成员,该类型是不可为空的值类型。程序崩溃了:

Nullable<Int32> maxTagFrequency = (from t in tagSummary select t.tagCount).Max();

这很奇怪,因为我声明变量可以为空,int? maxTagFrequency也不起作用......

整个LINQ查询:

private void BindTagCloud()
{

 int pro_id = Convert.ToInt32(proj_id);

    var tagSummary = from af in db.AgileFactors
               join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
               join s in db.Stories on psf.StoryID equals s.StoryID 
               join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
               join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
               join pro in db.Projects on it.ProjectID equals pro.ProjectID
               where pro.ProjectID == pro_id &&
                     pro.ProjectID == it.ProjectID &&
                     it.ProjectIterationID == pim.ProjectIterationID &&
                     pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                     s.StoryID == psf.StoryID &&
                     psf.AgileFactorID == af.AgileFactorID
                     group af by af.Name into tagGroup

                     select new
                     {

                        Tag = tagGroup.Key,
                        tagCount = tagGroup.Count()

                     };

    Nullable<Int32> maxTagFrequency = (from t in tagSummary select t.tagCount).Max();

var tagCloud = from af in db.AgileFactors
                   join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                   join s in db.Stories on psf.StoryID equals s.StoryID
                   join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                   join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                   join pro in db.Projects on it.ProjectID equals pro.ProjectID
                   where pro.ProjectID == pro_id &&
                         pro.ProjectID == it.ProjectID &&
                         it.ProjectIterationID == pim.ProjectIterationID &&
                         pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                         s.StoryID == psf.StoryID &&
                         psf.AgileFactorID == af.AgileFactorID
                   group af by af.Name into tagGroup
                   select new
                   {

                       Tag = tagGroup.Key,
                       weight = (double)tagGroup.Count() / maxTagFrequency * 100
                   };

  ListView1.DataSource = tagCloud; 
  ListView1.DataBind();

}

3 个答案:

答案 0 :(得分:10)

试试这个:

int? maxTagFrequency = (from t in tagSummary select (int?)t.tagCount).Max();

当你将强制转换放在linq查询中时,它允许整个结果在必要时为null。

我搜索了“空序列中的linq max”并且以下链接是相关的:Max or Default?

特别是在该页面上是本文的链接 - 这提供了有关其工作原理的更详细说明:http://www.interact-sw.co.uk/iangblog/2007/09/10/linq-aggregates

答案 1 :(得分:1)

从使用SQL的X.*功能的存储过程中提取LINQ to SQL时会发生此错误。

明确声明SELECT子句中的每个字段都可以解决此问题。

而不是

SELECT * from X

这将在某些情况下解决错误

SELECT X.Field1, X.Field2 from X

特别是,当在可空整数字段上定义了外键关系时,有时会出现此错误。

答案 2 :(得分:0)

这也可能是由于通过设计人员在DBML上创建的无效关联引起的,或者就此而言。在我的情况下,当我试图创建一对一的表时,我发现了一个错误,我把错误的表作为父表。所以发生的事情是我的Linq DBML类期望来自DBML中定义的“父”的记录。由于该“父”表实际上不是父表,因此它通常没有在链接两者的id列上关联的相应记录。所以当发生这种情况时,我的Linq类的填充在构造/填充结果集中的第3行时会出错。