使用LINQ从DataTable中删除特定的行

时间:2021-04-17 09:16:41

I want to delete a particular row from a DataTable named dt.

我想从名为dt的DataTable中删除特定行。

For a table in SQL, I could do something like:

对于SQL中的表,我可以这样做:

DELETE FROM dt 
WHERE BASELINE_FOLDER = baselineSubfolder
AND BASELINE_FILE = baselineFilename
AND BASELINE_CHECKSUM = baselineChecksum;

Is there an equivalent LINQ statement for this?

是否有相应的LINQ语句?

2 个解决方案

#1


2  

Assuming you don't have the model's and only a DataTable (this is what I understand from the OP).

假设您没有模型,只有DataTable(这是我从OP中理解的)。

//Cast to enumerable of `DataRow` and filter on your condition
var rows = dt.Rows.Cast<DataRow>().Where(row => row["BASELINE_FOLDER"] == baselineSubFolder && row["BASELINE_FILE" == baselineFilename
&& row["BASELINE_CHECKSUM"] == baselineChecksum).ToArray();
//Loop through and remove the rows that meet the condition
foreach(DataRow dr in rows)
{
  dt.Rows.Remove(dr);
}

#2


0  

you can convert the data table to list and can use RemoveAt() to do so.

您可以将数据表转换为列表,并可以使用RemoveAt()来执行此操作。

You can convert it to list and use the below code

您可以将其转换为列表并使用以下代码

string baseLineFolder=dt.Rows["BASELINE_FOLDER"].ToString();
                string baseLineFile=dt.Rows["BASELINE_FILE"].ToString();
                string baseLineChecksum=dt.Rows["BASELINE_CHECKSUM"].ToString();
                var dtresult = dt.AsEnumerable();
                var result=(from r in dtresult
                            where(r.Field<string>("BASELINE_FOLDER")!=baseLineFolder)
                            &&(r.Field<string>("BASELINE_FILE")!=baseLineFile)
                            &&(r.Field<string>("BASELINE_CHECKSUM ")!=baseLineChecksum)
                              select r).ToList();

#1


2  

Assuming you don't have the model's and only a DataTable (this is what I understand from the OP).

假设您没有模型,只有DataTable(这是我从OP中理解的)。

//Cast to enumerable of `DataRow` and filter on your condition
var rows = dt.Rows.Cast<DataRow>().Where(row => row["BASELINE_FOLDER"] == baselineSubFolder && row["BASELINE_FILE" == baselineFilename
&& row["BASELINE_CHECKSUM"] == baselineChecksum).ToArray();
//Loop through and remove the rows that meet the condition
foreach(DataRow dr in rows)
{
  dt.Rows.Remove(dr);
}

#2


0  

you can convert the data table to list and can use RemoveAt() to do so.

您可以将数据表转换为列表,并可以使用RemoveAt()来执行此操作。

You can convert it to list and use the below code

您可以将其转换为列表并使用以下代码

string baseLineFolder=dt.Rows["BASELINE_FOLDER"].ToString();
                string baseLineFile=dt.Rows["BASELINE_FILE"].ToString();
                string baseLineChecksum=dt.Rows["BASELINE_CHECKSUM"].ToString();
                var dtresult = dt.AsEnumerable();
                var result=(from r in dtresult
                            where(r.Field<string>("BASELINE_FOLDER")!=baseLineFolder)
                            &&(r.Field<string>("BASELINE_FILE")!=baseLineFile)
                            &&(r.Field<string>("BASELINE_CHECKSUM ")!=baseLineChecksum)
                              select r).ToList();