【问题标题】:Count returns Zero asp.net计数返回零 asp.net
【发布时间】:2015-04-24 13:52:11
【问题描述】:

我有一张表格,其中包含地区、地区、投诉和案件状态。我想做的是:

  • 创建表的 3 个实例
  • 使用一个作为主表,每个区域用于比较另一个 2
  • 一张表将统计每个地区与主实例匹配的所有投诉 -第三个计算所有案例状态,然后将所有三个结果插入另一个表中

这是目前为止的代码。问题是计数总是返回 0,这会引发错误。

foreach (DataRow mainrow in CaseTable.Rows)
{
    RegionName = mainrow.Field<string>("Region");
    DistrictName = mainrow.Field<string>("DistrictName");

    if (mainrow.Field<string>("CaseStatus") == "Resolved")
        DistrictcaseCount++;
    DistrictcaseCount++;
    PercentageResolved = (DistrictResolvedCount / DistrictResolvedCount) * 100;
    PercResolved = PercentageResolved.ToString() + "%";

    ReportTable.Rows.Add(RegionName, DistrictName, DistrictcaseCount, DistrictResolvedCount, PercResolved);
}

【问题讨论】:

  • 看看你的代码,我可以告诉你,如果你将一个变量自己除以它总是1,所以percentageResolved总是100
  • @Dom not 当该值为 0 时,您会得到 OP 所指的“引发错误”。
  • 在计算 PercentageResolved 时,不应该除以 DistrictcaseCount 吗?
  • DistrictResolvedCount 在这个方法中没有定义,所以很难回答。

标签: c# asp.net foreach filter


【解决方案1】:

您的问题中的代码 sn-p 似乎存在多个问题。

  1. 您正在递增 DistrictcaseCount 而不是 DistrictResolvedCount
  2. 您通过将DistrictResolvedCount 除以自身来计算百分比。
  3. 您正在执行除法,对于DistrictResolvedCount / DistrictcaseCount,它始终为 0,因为它们是整数。
  4. 您将在每个案例之后添加一个新行。

我相信这会如你所愿:

// This will count cases and resolved cases, grouped by district.
var districtGroups = 
    from row in CaseTable.Rows
    group row by new { 
        Region = row.Field<string>("Region"), 
        District = row.Field<string>("DistrictName") 
    } into g
    select new {
        Region = g.Key.Region,
        District = g.Key.District,
        CaseCount = g.Count(),
        ResolvedCaseCount = g.Count(r => "Resolved".Equals(r.Field<string>("CaseStatus")))
    };

// this will calculate the percentage and add the rows.
foreach (var entry in districtGroups)
{
    // determine percentage.
    string percentageResolved;
    if (entry.CaseCount > 0)
    {
        var percent = (100.0 * entry.ResolvedCaseCount) / CaseCount;
        percentageResolved = percent.ToString() + "%";
    }
    else // What to do if there are no cases? 
    {
        percentageResolved = "N/A";
    }

    // add row for the district.
    ReportTable.Rows.Add(
        entry.Region, entry.District, 
        entry.CaseCount, entry.ResolvedCaseCount, 
        percentageResolved);
}

【讨论】:

  • 是的,我看到了我的错误并纠正了它。我得到了计数,但百分比仍然为 0。我将尝试此代码。它看起来很漂亮。我真是个菜鸟。谢谢大家
  • 我的代码仍然有问题。首先是 CaseTable.Rows.AsEnumerable()。我将其更改为 CaseTable.AsEnumberable() 没有产生错误。我似乎无法纠正的另一个错误是:ResolvedCaseCount = g.Count(r => "Resolved".Equals(e.Field("CaseStatus")))。错误来自 e。请帮忙
  • @Yaw 好的修复了它,我删除了AsEnumerable() 部分并将e 替换为r
【解决方案2】:

@Alex 我解决了这些问题。非常感谢你的代码。我最终得到了这个:

var districtGroups = 从 CaseTable.Rows.Cast() 中的行 按新分组行 { Region = row.Field("Region"), District = row.Field("DistrictName") } 到 g 选择新的 { 区域 = g.Key.Region, 区 = g.Key.District, CaseCount = g.Count(), ResolvedCaseCount = g.Count(r => "Resolved".Equals(r.Field("CaseStatus"))) };

        // this will calculate the percentage and add the rows.
        foreach (var entry in districtGroups)
        {
            // determine percentage.
            string percentageResolved;
            if (entry.CaseCount > 0)
            {

                var percent = (100.0 * entry.ResolvedCaseCount) / entry.CaseCount;
                percentageResolved = percent.ToString() + "%";
            }
            else // What to do if there are no cases? 
            {
                percentageResolved = "N/A";
            }

            // add row for the district.
            ReportTable.Rows.Add(
                entry.Region, entry.District,
                entry.CaseCount, entry.ResolvedCaseCount,
                percentageResolved);
        }

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多