C# – LINQ: System.Int32 is a non-nullable value type

clinqlinq-to-sqlnet

Error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. The program crashes here:

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

It's strange, because I declared the variable to be nullable, int? maxTagFrequency doesn't work either…

Entire LINQ Query:

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

}

Best Answer

Try this:

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

When you put the cast inside the linq query it allows the whole result to be null if necessary.

I did a search for "linq max on empty sequence" and the following link is relevant: Max or Default?

In particular on that page is a link to this article - This offers a more detailed explanation of why this works: http://www.interact-sw.co.uk/iangblog/2007/09/10/linq-aggregates