Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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# 使用LINQ更新数据表中的两列_C#_Datatable - Fatal编程技术网

C# 使用LINQ更新数据表中的两列

C# 使用LINQ更新数据表中的两列,c#,datatable,C#,Datatable,我想使用LINQ查询在一行中更新DataTable的两列。目前,我使用以下两行代码来执行相同的操作: oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>().ForEach(r => r["startdate"] = stDate); oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<Dat

我想使用LINQ查询在一行中更新DataTable的两列。目前,我使用以下两行代码来执行相同的操作:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>().ForEach(r => r["startdate"] = stDate);
oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>().ForEach(r => r["enddate"] = enDate);
oldSP.Select(string.Format(“[itemGuid]='{0}',itemGuid)).ToList().ForEach(r=>r[“startdate”]=stDate);
oldSP.Select(string.Format(“[itemGuid]='{0}',itemGuid)).ToList().ForEach(r=>r[“enddate”]=enDate);
如何在一行中使用一个
选择

尝试以下操作:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid)).ToList<DataRow>()
     .ForEach(r => { r["startdate"] = stDate; r["enddate"] = enDate; });
oldSP.Select(string.Format(“[itemGuid]='{0}',itemGuid)).ToList()
.ForEach(r=>{r[“startdate”]=stDate;r[“enddate”]=enDate;});

使用花括号在更多操作中执行两项操作:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid))
       .ToList<DataRow>()
       .ForEach(r => { r["enddate"] = enDate); r["startdate"] = stDate; });
oldSP.Select(string.Format(“[itemGuid]='{0}',itemGuid))
托利斯先生()
.ForEach(r=>{r[“enddate”]=enDate;r[“startdate”]=stDate;});

但为了代码可读性,我会使用老式的
foreach
循环。

您可以在一行中完成,只需将适当的操作委托传递给foreach方法:

oldSP.Select(string.Format("[itemGuid] = '{0}'", itemGuid))
     .ToList<DataRow>()
     .ForEach(r => { 
        r["startdate"] = stDate;
        r["enddate"] = enDate;
      });
oldSP.Select(string.Format(“[itemGuid]='{0}',itemGuid))
托利斯先生()
.ForEach(r=>{
r[“开始日期”]=开始日期;
r[“结束日期”]=结束日期;
});
此外,您还可以使用LINQ到数据集(对我来说,这比一行代码更易读):

var rowsToUpdate=
oldSP.AsEnumerable()。其中(r=>r.Field(“itemGuid”)==itemGuid);
foreach(rowsToUpdate中的变量行)
{
行设置字段(“起始日期”,起始日期);
行设置字段(“结束日期”,结束日期);
}

我不喜欢我在网上看到的任何例子,所以这里是我的例子

        DataTable dt = new DataTable();
        dt.Columns.Add("Year");
        dt.Columns.Add("Month");
        dt.Columns.Add("Views");
        for (int year = 2011; year < 2015; year++)
        {
            for (int month = 1; month < 13; month++)
            {
                DataRow newRow = dt.NewRow();
                newRow[0] = year;
                newRow[1] = month;
                newRow[2] = 0;
                dt.Rows.Add(newRow);
            }
        }

        dataGridView1.DataSource = dt;

        //if using Lambda 
        //var test = dt.AsEnumerable().Where(x => x.Field<string>("Year") == "2013" && x.Field<string>("Month") == "2").ToList();

        var test = (from x in dt.AsEnumerable()
                    where x.Field<string>("Year") == "2013"
                    where x.Field<string>("Month") == "2"
                    select x).ToList();


        test[0][0] = "2015";

        dt.AcceptChanges();
DataTable dt=newdatatable();
dt.列。添加(“年度”);
dt.列添加(“月份”);
dt.列。添加(“视图”);
对于(国际年份=2011年;年份<2015年;年份++)
{
对于(整数月=1;月<13;月++)
{
DataRow newRow=dt.newRow();
newRow[0]=年份;
newRow[1]=月份;
新行[2]=0;
dt.Rows.Add(newRow);
}
}
dataGridView1.DataSource=dt;
//如果使用Lambda
//var test=dt.AsEnumerable()。其中(x=>x.Field(“年”)==“2013”和&x.Field(“月”)==“2”).ToList();
var test=(从dt.AsEnumerable()中的x开始)
其中x.Field(“年份”)=“2013”
其中x.Field(“月”)=“2”
选择x).ToList();
测试[0][0]=“2015”;
dt.AcceptChanges();

//如果写入sql,请改用dt.SubmitChanges()

这种“单行”方法有时不像本例中那样可读性差、效率低。使用linq查询和foreach循环。@TimSchmelter我有一个巨大的数据表(1000条记录),使用多行方法比单行方法需要更多的时间。你把代码中的行与cpu周期混淆了。一行代码所花费的时间是100行代码的1000倍。始终首先使用可读性和可维护性最好的方法。@TimSchmelter在这里我们将要进行选择的每一行中,1000次选择不是比2000次选择好吗?您可以简单地存储结果,因为您选择了两次相同的数据行。但是
ToList
是多余的,因为
DataTable.Select
已经返回了一个集合,因此您可以免费将所需内存加倍。您接受的
LINQ
方法根本不需要另一个集合(除了
DataTable
)。foreach的
foreach
直接枚举查询。您还可以枚举
foreach
中的
DataTable.Rows
,并使用
if(condition…
检查是否必须更新此行。这将是最有效和最直接的方法。问题清楚地表明“使用LINQ更新数据表中的两列”,并且您的解决方案不基于LINQ。
        DataTable dt = new DataTable();
        dt.Columns.Add("Year");
        dt.Columns.Add("Month");
        dt.Columns.Add("Views");
        for (int year = 2011; year < 2015; year++)
        {
            for (int month = 1; month < 13; month++)
            {
                DataRow newRow = dt.NewRow();
                newRow[0] = year;
                newRow[1] = month;
                newRow[2] = 0;
                dt.Rows.Add(newRow);
            }
        }

        dataGridView1.DataSource = dt;

        //if using Lambda 
        //var test = dt.AsEnumerable().Where(x => x.Field<string>("Year") == "2013" && x.Field<string>("Month") == "2").ToList();

        var test = (from x in dt.AsEnumerable()
                    where x.Field<string>("Year") == "2013"
                    where x.Field<string>("Month") == "2"
                    select x).ToList();


        test[0][0] = "2015";

        dt.AcceptChanges();