0

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

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

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

Is there an equivalent LINQ statement for this?

2
  • Are you using Linq to DataSet or something similar? Commented Mar 20, 2013 at 15:43
  • tbh I don't know, so probably not. I'm brand new to LINQ and have never really used it before aside from a couple of copy-and-paste solutions I found online Commented Mar 20, 2013 at 15:47

2 Answers 2

2

Assuming you don't have the model's and only a DataTable (this is what I understand from the 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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is giving me an error on the cast: 'System.Linq.Queryable.Cast<TResult>(System.Linq.IQueryable)' is a 'method', which is not valid in the given context\
@user1985189 I missed the () at first off of Cast, I have changed this now. You may need to check what references you have.
Ok thanks it compiles now. But doesn't removing a row in a foreach loop screw up the counter (because you're deleting a row during the looping process so it no longer knows how many items to loop through?) When I run it now it gives this error: Collection was modified; enumeration operation might not execute.
@user1985189 your correct, please call ToArray() on the LINQ query :)
ok this works, but there's one more thing I didn't mention in my original question. Another condition is that another column (STATUS) must be blank/empty. However, when I add in the line && row["STATUS"] == "" it doesn't execute the dt.Rows.Remove(dr); line. Same thing if i use null instead of ""
|
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.