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?
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);
}
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();
Related
I have a data table that includes a column holding IP addresses. I need to be able to filter the rows and remove all rows where IP address is in the range of, say, from 10.20.1.xxx to 10.20.15.xxx as well as 10.20.61.xxx. (i.e. exclude 10.20.1.xxx, 10.20.2.xxx, ..., 10.20.14.xxx, 10.20.15.xxx and 10.20.61.xxx).
I would do the following,
DataTable dt; //your datatable here
DataView dv = dt.DefaultView;
foreach (DataRow dr in dt.Rows)
{
if (Regex.IsMatch(dr["Column name of your IP"].ToString(), "regex to check IP") == false)
{
//Delete that row or something
}
else
{
//Do something else
}
}
DataTable tempTable = dv.ToTable();
//where temptable is your sorted and updated datatable
You can store this inside a method and call it to perform a check wherever you are binding or before that.
SQL
DELETE FROM sometable WHERE ip LIKE '10.20.1.%'
or
DELETE FROM sometable WHERE ip LIKE '10.20.%'
Linq
Without any code from you on how you're getting data from DB this will just get you a list of rows to be deleted, you can take it from there
var itemsToDelete = someList.Where(x => x.Ip.StartsWith("10.20.1"));
This worked for me:
Newtable = (from r in table.AsEnumerable()
where (
Convert.ToInt32(r.Field<string>("IP").Split('.')[2]) > 15 &&
Convert.ToInt32(r.Field<string>("IP").Split('.')[2]) != 61)
select r).CopyToDataTable();
I have a datatable with 4 columns
- A|1|Apple|Turnip
- A|2|Water|Fruit
- B|1|Water|Orange
- B|2|Water|Mango
- C|1|Hello|World
What I want to do is select distinct column A. Then I want to select distinct columns 2,3,4 based on column A.
This is what i'm using to select distinct column A. My question with this first part is how do you order by column A (linkControl). Member is my datatable, and this returns every distrinct column a
var linkControl = Member.AsEnumerable()
.Select(row => new
{
linkCon = row.Field<string>("LinkControl")
})
.Distinct();
Then because I want to iterate each of the linkCons I do the following, which only returns one datarow even though there is about 20 datarows for each linkCon. How do i return all the rows?
foreach (var linkC in linkControl)
{
var linkControllink = (from DataRow dr in ADMemberships.Rows
where dr["LinkControl"] == linkC.linkCon
orderby (string)dr["URLDescription"]
select dr);
}
foreach (var lcl in linkControllink)
{
//only has one row in it
}
I had to change the code to where dr["LinkControl"].ToString() == linkC.linkCon.ToString() and i dont really understand why.
I have a DataTable named dtEmployee with four columns, viz. EmployeeId, EmployeePosition, SupervisorPosition, SupervisorId which all are of type varchar(10).
I want to filter the results in dtEmployee whose result is equivalent to SQL Query below.
Select * from dtEmployee where EmployeePosition not in (Select distinct SupervisorPosition
from dtEmployee);
I have achieved the sub query equivalent by creating another DataTable named dtDistinctSupervisors as
dtDistinctSupervisors = dtEmployee.DefaultView.ToTable(true, "SupervisorPosNum");
This is equivalent to Select distinct SupervisorPosition
from dtEmployee
How can I get the whole query equivalent.
Your help is appreciated. Thanks.
Merin
Having Distinct inside an IN subquery makes no difference. This should dot it:
var dt = dtEmployee.AsEnumerable();
var result = dt.Where(emp => !dt.Any(sup =>
sup.Field<string>("SupervisorPosition") ==
emp.Field<string>("EmployeePosition")));
And to show the result in the Console:
foreach (DataRow row in result) // Loop over the rows.
{
Console.WriteLine("--- Row ---");
foreach (var item in row.ItemArray)
{
Console.Write("Item: ");
Console.WriteLine(item);
}
}
I have a DataSet with many DataTables each containing many columns plus a column buildingID.
I would like to filter the entire DataSet by giving it a value for buildingID. I would like the rows in every table with a buildingID of, say 343.
Is there any quick possible way in C#?
You can use DataTable.Select, which returns filtered rows from a DataTable matching a criteria.
foreach (DataTable table in dataset.Tables) {
var rows = table.Select("buildingID = " + buildingId.ToString());
// Do stuff with filtered rows
}
To easily get all the rows that match your criteria, here's a LINQ expression:
var rows = dataset.Tables.SelectMany(
t => t.Select("buildingID = " + buildingId.ToString()));
What about this?
var ds1 = new DataSet();
foreach (DataTable dt in ds1.Tables)
{
var filtereddt = dt.AsEnumerable().Where(row => row.Field<int>("buildingID") == 1).ToList();
//you can add these lists to another list array or something like that.
}
i have Dataset that contain any database.
how to run any query on this dataset ?
You could use a DataView:
var view = new DataView(dataSet.Tables("TableName"));
view.RowFilter = "ColumnName LIKE '%something%'"
foreach (var row in view.Rows)
{
// do something
}
or LINQ:
var results = from row in dataSet.Tables("Table").AsEnumerable()
where row.Field(Of String)("ColumnName").Contains("something")
select row;
foreach (var row in results)
{
// do something
}
Google should suffice for a question like this...
Hi I believe you can use Linq To Objects to query this dataset:
var results = (from r in mydatasetvar.AsQuarieable() .. )
See this for more info
Alternatively, you can loop through the tables, rows and columns of the dataset.