好的,我正在使用LINQ根据我的数据库中的标签生成标签云。我有一个问题,我似乎无法弄明白。我第一次添加标签时,它以最大的字体显示(36pt) - 我想因为计算的%值,权重取值为100并传递给GetTagSize()。有没有办法在我这样做的地方放置一个if语句:weight =(double)tagGroup.Count()/ maxTagFrequencyNegative * 100 检查计数为1并显示最小字体?谢谢你的帮助!
var tagSummaryNegative = 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.StoryCategoryID == 1 &&
s.StoryID == psf.StoryID &&
psf.AgileFactorID == af.AgileFactorID
group af by af.Name into tagGroup
select new
{
Tag = tagGroup.Key,
tagCount = tagGroup.Count()
};
int maxTagFrequencyNegative = (from t in tagSummaryNegative select (int?)t.tagCount).Max() ?? 0;
var tagCloudNegative = 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.StoryCategoryID == 1 &&
s.StoryID == psf.StoryID &&
psf.AgileFactorID == af.AgileFactorID
group af by af.Name into tagGroup
select new
{
Tag = tagGroup.Key,
weight = (double)tagGroup.Count() / maxTagFrequencyNegative * 100
};
public string GetTagSize(double weight)
{
if (weight >= 99)
return "36pt";
else if (weight >= 80)
return "29pt";
else if (weight >= 64)
return "23pt";
else if (weight >= 48)
return "18pt";
else if (weight >= 32)
return "14pt";
else if (weight >= 10)
return "11pt";
else
return "8pt";
}
答案 0 :(得分:0)
只需使用?:
运算符:
weight = (tagGroup.Count() == 1) ? smallestFont : ((double)tagGroup.Count() / maxTagFrequencyNegative * 100)
答案 1 :(得分:0)
尝试:
weight = (tagGroup.Count() == 1) ? (double)1 : (double)tagGroup.Count() / maxTagFrequencyNegative * 100
我不确定这是否正是您所需要的,但基本上您可以使用条件运算符来解决此问题。第一个参数将是最小的字体,不知道它代表什么,所以我将其设置为1。