C# 对数据表中的不同值求和
我有这个数据表:C# 对数据表中的不同值求和,c#,datatable,C#,Datatable,我有这个数据表: SECTION CODE DATE VALUE section1 20 jul 2013 31507976.71 section1 20 nov 2013 31643256.16 section1 20 dec 2013 28788554.96 section1 20 jan 2014 32297021.88 section1 20 feb 2014 29111554.83 sectio
SECTION CODE DATE VALUE
section1 20 jul 2013 31507976.71
section1 20 nov 2013 31643256.16
section1 20 dec 2013 28788554.96
section1 20 jan 2014 32297021.88
section1 20 feb 2014 29111554.83
section1 20 mar 2014 32789670.01
section1 20 apr 2014 32045399.52
section1 20 may 2014 33420106.37
section1 20 jun 2014 31246337.7
section1 20 jul 2014 31406327.47
section2 50 apr 2013 31219430.46
section2 50 may 2013 32200204.19
section2 50 jun 2013 32348605.39
section2 50 dec 2013 33158170.04
section2 50 jan 2014 33472813.18
section2 50 feb 2014 30875748.12
section2 50 mar 2014 34655429.31
section2 50 apr 2014 31894634.74
section2 50 may 2014 29467627.18
section2 50 jun 2014 31224988.16
section2 50 jul 2014 59455468.22
section3 20 apr 2013 29467627.18
section3 20 nov 2013 28230480.06
section3 20 dec 2013 27273436.72
section3 20 jan 2014 27074471.34
section3 20 may 2014 31396504.33
section3 20 jun 2014 29528727.08
section3 20 jul 2014 29630946.19
我想得到一个总的特定代码为每个时期。
例如,对于代码20,它将计算第二列中代码为20的每个部分,并按每个期间求和。
我应该得到这样一个列表:
Period Code: 20
apr 2013 29467627.18
apr 2014 32045399.52
dec 2013 56061991.68
feb 2014 29111554.83
jan 2014 59371493.22
jul 2013 31507976.71
jul 2014 61037273.66
jun 2013 0
jun 2014 60775064.78
mar 2014 32789670.01
may 2013 0
may 2014 64816610.7
nov 2013 59873736.22
public class SectionModel
{
public string SectionId { get; set; }
public int Code { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
var list = dt.Rows.Select(d => new SectionModel
{
SectionId = d[0],
Code = Convert.ToInt32(d[1]),
Date = Convert.ToDateTime(d[2]),
Value = Convert.ToDecimal(d[3])
});
var results = list.GroupBy(row => new {row.Date, row.Code}, (key, rows) => new
{
period = key.Date,
code = key.Code,
value = rows.Sum(r => r.Value)
})
.Where(r => r.code == codeParameter)
.ToList();
var aggregatedItems = items.GroupBy(x => new { x.Code, x.Period })
.Select(g => new
{
Code = g.Key.Code,
Period = g.Key.Period,
Value = g.Sum(y => y.Value)
});
这是我尝试过的,但我只得到零值:
string code="20";
List<string[]> data = new List<string[]>();
List<string> period = new List<string>();
List<decimal> value = new List<decimal>();
decimal nr=0;
foreach (DataRow dr in dt.Rows)
if (dr[1].ToString().Equals(code))
period.Add(dr[2].ToString());
}
List<string>period2= period.Distinct().ToList();
foreach (string p in period2) value.Add(0);
for (int i = 0; i < period2.Count; i++)
{
foreach (DataRow dr in dt.Rows)
{ if (period2[i].Equals(dr[2].ToString()))
nr += Convert.ToDecimal(dr[3].ToString());
}
value[i] = nr;
}
for (int i = 0; i < period2.Count; i++)
{
data.Add(new string[] {period2[i], value[i].ToString()});
}
string code=“20”;
列表数据=新列表();
列表周期=新列表();
列表值=新列表();
十进制nr=0;
foreach(数据行dr在dt.行中)
if(dr[1].ToString().Equals(代码))
period.Add(dr[2].ToString());
}
Listperiod2=period.Distinct().ToList();
foreach(period2中的字符串p)值。添加(0);
for(int i=0;i
我认为这可能会让您选择代码为20且日期大于2013年4月的所有行
List<DataRow> result = urDataTable.Select("CODE == 20 AND DATE > #1/4/2013#");
foreach (DataRow row in result)
{
//do
}
List result=urdata表。选择(“code==20和日期>#1/4/2013#”);
foreach(结果中的数据行)
{
//做
}
如果
code
是非数字的,请小心放置20
如'20'
我认为这可能会让您选择所有代码为20且日期大于2013年4月的行
List<DataRow> result = urDataTable.Select("CODE == 20 AND DATE > #1/4/2013#");
foreach (DataRow row in result)
{
//do
}
List result=urdata表。选择(“code==20和日期>#1/4/2013#”);
foreach(结果中的数据行)
{
//做
}
如果
code
是非数字的,请小心放置20
如'20'
您可以按日期和代码对结果进行分组。
然后,您可以通过代码对它们进行过滤,以获得所需的值
我建议首先将结果映射到模型,这样比通过索引引用字符串数组更容易阅读
大概是这样的:
Period Code: 20
apr 2013 29467627.18
apr 2014 32045399.52
dec 2013 56061991.68
feb 2014 29111554.83
jan 2014 59371493.22
jul 2013 31507976.71
jul 2014 61037273.66
jun 2013 0
jun 2014 60775064.78
mar 2014 32789670.01
may 2013 0
may 2014 64816610.7
nov 2013 59873736.22
public class SectionModel
{
public string SectionId { get; set; }
public int Code { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
var list = dt.Rows.Select(d => new SectionModel
{
SectionId = d[0],
Code = Convert.ToInt32(d[1]),
Date = Convert.ToDateTime(d[2]),
Value = Convert.ToDecimal(d[3])
});
var results = list.GroupBy(row => new {row.Date, row.Code}, (key, rows) => new
{
period = key.Date,
code = key.Code,
value = rows.Sum(r => r.Value)
})
.Where(r => r.code == codeParameter)
.ToList();
var aggregatedItems = items.GroupBy(x => new { x.Code, x.Period })
.Select(g => new
{
Code = g.Key.Code,
Period = g.Key.Period,
Value = g.Sum(y => y.Value)
});
您可以按日期和代码对结果进行分组。 然后,您可以通过代码对它们进行过滤,以获得所需的值 我建议首先将结果映射到模型,这样比通过索引引用字符串数组更容易阅读 大概是这样的:
Period Code: 20
apr 2013 29467627.18
apr 2014 32045399.52
dec 2013 56061991.68
feb 2014 29111554.83
jan 2014 59371493.22
jul 2013 31507976.71
jul 2014 61037273.66
jun 2013 0
jun 2014 60775064.78
mar 2014 32789670.01
may 2013 0
may 2014 64816610.7
nov 2013 59873736.22
public class SectionModel
{
public string SectionId { get; set; }
public int Code { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
var list = dt.Rows.Select(d => new SectionModel
{
SectionId = d[0],
Code = Convert.ToInt32(d[1]),
Date = Convert.ToDateTime(d[2]),
Value = Convert.ToDecimal(d[3])
});
var results = list.GroupBy(row => new {row.Date, row.Code}, (key, rows) => new
{
period = key.Date,
code = key.Code,
value = rows.Sum(r => r.Value)
})
.Where(r => r.code == codeParameter)
.ToList();
var aggregatedItems = items.GroupBy(x => new { x.Code, x.Period })
.Select(g => new
{
Code = g.Key.Code,
Period = g.Key.Period,
Value = g.Sum(y => y.Value)
});
您可以通过如下方式使用
GroupBy
获得所需信息:
Period Code: 20
apr 2013 29467627.18
apr 2014 32045399.52
dec 2013 56061991.68
feb 2014 29111554.83
jan 2014 59371493.22
jul 2013 31507976.71
jul 2014 61037273.66
jun 2013 0
jun 2014 60775064.78
mar 2014 32789670.01
may 2013 0
may 2014 64816610.7
nov 2013 59873736.22
public class SectionModel
{
public string SectionId { get; set; }
public int Code { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
var list = dt.Rows.Select(d => new SectionModel
{
SectionId = d[0],
Code = Convert.ToInt32(d[1]),
Date = Convert.ToDateTime(d[2]),
Value = Convert.ToDecimal(d[3])
});
var results = list.GroupBy(row => new {row.Date, row.Code}, (key, rows) => new
{
period = key.Date,
code = key.Code,
value = rows.Sum(r => r.Value)
})
.Where(r => r.code == codeParameter)
.ToList();
var aggregatedItems = items.GroupBy(x => new { x.Code, x.Period })
.Select(g => new
{
Code = g.Key.Code,
Period = g.Key.Period,
Value = g.Sum(y => y.Value)
});
之后,您可以这样访问它们(订购是可选的):
.您可以通过使用
GroupBy
获得所需信息,如下所示:
Period Code: 20
apr 2013 29467627.18
apr 2014 32045399.52
dec 2013 56061991.68
feb 2014 29111554.83
jan 2014 59371493.22
jul 2013 31507976.71
jul 2014 61037273.66
jun 2013 0
jun 2014 60775064.78
mar 2014 32789670.01
may 2013 0
may 2014 64816610.7
nov 2013 59873736.22
public class SectionModel
{
public string SectionId { get; set; }
public int Code { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
var list = dt.Rows.Select(d => new SectionModel
{
SectionId = d[0],
Code = Convert.ToInt32(d[1]),
Date = Convert.ToDateTime(d[2]),
Value = Convert.ToDecimal(d[3])
});
var results = list.GroupBy(row => new {row.Date, row.Code}, (key, rows) => new
{
period = key.Date,
code = key.Code,
value = rows.Sum(r => r.Value)
})
.Where(r => r.code == codeParameter)
.ToList();
var aggregatedItems = items.GroupBy(x => new { x.Code, x.Period })
.Select(g => new
{
Code = g.Key.Code,
Period = g.Key.Period,
Value = g.Sum(y => y.Value)
});
之后,您可以这样访问它们(订购是可选的):
.我直觉上认为您需要使用==而不是等式我还不确定,为什么要为
nr
变量重设,每次迭代行之前。我直觉上认为您需要使用==而不是等式我还不确定,为什么要为零赋值,但是在迭代行之前,每次都应该重置nr
变量。周期是随机的,可能小于2013年4月。我只放了datatable的一小部分。但我需要为一个代码获取数据表中存在的所有周期,即使对于代码为零值的周期也是如此。周期是随机的,可能小于2013年4月。我只放了datatable的一小部分。但我需要为一个代码获取数据表中存在的所有周期,即使对于代码值为零值的周期也是如此。