【发布时间】:2015-05-25 07:57:28
【问题描述】:
我有一张如下所述的表格
ID Group Dept Percentage
1 Science H2 100
2 History Y5 0
2 History Y5 50
2 History Y6 100
3 Econimcs E9 100
现在下面的代码将检查 ID 的总和是 0 还是 100。但是现在我需要在 ID 列中包含 Group 和 Dept 列来检查总和。
var resultPCT = from row in dtNewL3.AsEnumerable()
group row by row["ID"]
into g
select new
{
Code = g.Key,
NewPCT = g.Sum(x => int.Parse(x["Percentage"].ToString()))
};
var errorPCT = resultPCT.Where(x => x.NewPCT != 100 && x.NewPCT != 0);
if (errorPCT.Any())
{
var CC = errorPCT.Select(x => x.Code);
string strAlert = string.Format("ID(s) {0} should have sum of 0 or 100 in Percentage.", string.Join(",", CC));
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + strAlert + "')", true);
return;
}
考虑到上表,此代码将给出警告,指出 ID 2 的总和不是 0 或 100。
上面的代码将仅根据 ID 检查 0 或 100 的总和。但现在我需要一起检查 0 或 100 的 ID、Group 和 Dept 的总和。现在,在上表中,ID - 2 和 Group History 和 Dept - Y5 应该会弹出一个警报,说明它的总和不是 0 或 100。
【问题讨论】:
-
请更好地描述您手头的任务。不是用代码,而是用文字。喜欢:我有这张表,对于有东西的行,我需要返回一些东西。或者对于具有相同 id 的行组,其中百分比之和等于某事.. 等等。不清楚
-
@AlexanderTaran - 我更新了我的帖子。请看一下。
-
听起来像是一个作业:/