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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19855772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:00:51  来源:igfitidea点击:

Update two columns in a DataTable using LINQ

c#datatable

提问by Jainendra

I want to update two columns of DataTable in a single line using LINQ query. Currently I am using following two lines to do the same:

我想使用 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);

How can I do this in one line, using one Select?

我怎样才能在一行中做到这一点,使用 one Select

采纳答案by Sergey Berezovskiy

You can do it in one 'line', just pass appropriate action delegate to ForEach method:

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

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

Also you can use LINQ to DataSet (looks more readable to me, than one-liner):

你也可以使用 LINQ to DataSet(对我来说看起来比单行更具可读性):

var rowsToUpdate = 
    oldSP.AsEnumerable().Where(r => r.Field<string>("itemGuid") == itemGuid);

foreach(var row in rowsToUpdate)
{
    row.SetField("startdate", stDate);
    row.SetField("enddate", enDate);
}

回答by Réda Mattar

Try this :

尝试这个 :

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

回答by gzaxx

Use curly bracers to do two on more operations:

使用花括号在更多操作上做两个:

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

But for code readability I would use old-fashioned foreachloop.

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

回答by user2959284

I didn't like any of the examples I saw on the web, so here's my example

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

        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();

//if writing to sql use dt.SubmitChanges() instead

//如果写入 sql 使用 dt.SubmitChanges() 代替