Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向datatable C中某个列和行的值添加值#_C#_Foreach_Datatable - Fatal编程技术网

C# 如何向datatable C中某个列和行的值添加值#

C# 如何向datatable C中某个列和行的值添加值#,c#,foreach,datatable,C#,Foreach,Datatable,我有两个数据表: 1.dtEmployee: |agent_id|agent_name|sum| 2.dtReport: |sale_date|agent_id|sum | ------------------------ 对于dtReport中的每个记录,我需要在dtEmployee中找到agent\u id,并将dtReport[“sum”]的值添加到dtEmployee[“sum”]: 有什么方法可以让我做到这一点吗?这可以通过多种方式实现 选项1: foreach(DataRow r

我有两个数据表:

1.
dtEmployee

|agent_id|agent_name|sum|
2.
dtReport

|sale_date|agent_id|sum |
------------------------
对于
dtReport
中的每个记录,我需要在
dtEmployee
中找到
agent\u id
,并将dtReport[“sum”]的值添加到dtEmployee[“sum”]:


有什么方法可以让我做到这一点吗?

这可以通过多种方式实现

选项1:

foreach(DataRow row in dtEmployee.Rows)
{
    var update = dtReport.AsEnumerable().FirstOrDefault(r => r.Field<string>("agent_id") == row.Field<string>("agent_id"));

    if(update !=null)
    row.SetField<float>("sum",  update.Field<float>("sum"));      
} 
工作


希望这有帮助

类似这样的方法很有效:

private void AddValue(string agent_id, decimal sum)
{
    DataRow[] row= dtEmployee.Select("agent_id= '"+agent_id+"'"); 
    //since only one record with this agent_id, we take first record of array -> row[0]
    decimal dSum= Convert.ToDecimal(row[0][column]);
    dSum+= sum;
    row[0]["sum"] = dSum;
}
并将此函数插入到循环中:

foreach (DataRow r in dtReport)
{
    AddValue(r["agent_id"], r["sum"]);
} 

你可以试试这样的。假设您的代理id和总和是整数

foreach (DataRow r in dtReport.Rows)
{
    dtEmployee.Select(string.Format("agent_id = {0}", r["agent_id"])).ToList<DataRow>().ForEach(
             v => { v["sum"] = (v.IsNull("sum") ? 0 : v.Field<int>("sum")) + (r.IsNull("sum") ? 0 : r.Field<int>("sum")); });
}
foreach(dtReport.Rows中的数据行r)
{
dtEmployee.Select(string.Format(“agent\u id={0}”,r[“agent\u id”])).ToList().ForEach(
v=>{v[“sum”]=(v.IsNull(“sum”)?0:v.Field(“sum”)+(r.IsNull(“sum”)?0:r.Field(“sum”);};
}
或等效代码

foreach (DataRow r in dtReport.Rows)
{
    DataRow[] empRow = dtEmployee.Select("agent_id = " + r["agent_id"]);
    for (int i = 0; i < empRow.Length; i++)
    {
        empRow[i]["sum"] = (empRow[i].IsNull("sum") ? 0 : (int)empRow[i]["sum"]) + (r.IsNull("sum") ? 0 : (int)r["sum"]);
    }
}
foreach(dtReport.Rows中的数据行r)
{
DataRow[]empRow=dtEmployee.Select(“agent_id=“+r[”agent_id“]);
for(int i=0;i
您只想在运行时执行此操作吗?。或者也要将此值保存在表中?是的,进行加法并将其保存到表中。好的,那么为什么要在c#中执行此操作?。您也可以在sql中执行此操作。是否有条件先在datatable中获取记录,然后保存回表中。
foreach (DataRow r in dtReport.Rows)
{
    dtEmployee.Select(string.Format("agent_id = {0}", r["agent_id"])).ToList<DataRow>().ForEach(
             v => { v["sum"] = (v.IsNull("sum") ? 0 : v.Field<int>("sum")) + (r.IsNull("sum") ? 0 : r.Field<int>("sum")); });
}
foreach (DataRow r in dtReport.Rows)
{
    DataRow[] empRow = dtEmployee.Select("agent_id = " + r["agent_id"]);
    for (int i = 0; i < empRow.Length; i++)
    {
        empRow[i]["sum"] = (empRow[i].IsNull("sum") ? 0 : (int)empRow[i]["sum"]) + (r.IsNull("sum") ? 0 : (int)r["sum"]);
    }
}