gpt4 book ai didi

c# - LINQ:System.Int32 是不可为 null 的值类型

转载 作者:太空狗 更新时间:2023-10-29 22:24:28 28 4
gpt4 key购买 nike

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

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

}

最佳答案

试试这个:

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

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

我搜索了“linq max on empty sequence”,以下链接是相关的:Max or Default?

特别是在那个页面上有一个指向这篇文章的链接 - 这提供了为什么这有效的更详细的解释:http://www.interact-sw.co.uk/iangblog/2007/09/10/linq-aggregates

关于c# - LINQ:System.Int32 是不可为 null 的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4788577/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com