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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With