简体   繁体   中英

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

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

}

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

This error can occur in LINQ to SQL when pulling from a stored procedure that uses the X.* functionality of SQL.

Explicitly stating every field in the SELECT clause can fix this problem.

Instead of

SELECT * from X

This will fix the error in some cases

SELECT X.Field1, X.Field2 from X

In particular, this error seems to occur sometimes when there is a foreign-key relationship defined on nullable integer fields.

This can also be caused by invalid associations that are created on the DBML through the designer, or otherwise for that matter. In my case I caught a mistake where I made the wrong table the parent when I was trying to actually create a one-to-one table. So what happened is that my Linq DBML class would expect a record from its "parent" as defined in the DBML. Since that "parent" table wasn't really a parent, it often didn't have a corresponding record associated on the id column that linked the two. So when this happened, the population of my Linq class would error while constructing/populating the 3rd row in my resultset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM