I have datatable with following columns.
ID
Name
Dept
I want to select Name where ID = "XXX" from datatable.
Can any one please suggest how can i do that.
dt.AsEnumerable().FirstOrDefault(row => row.Field<int>("ID") == 123);
Try this
DataRow[] rows = DataTable.Select("ID = 'XXX'")
Related
So I have a DataTable which uses:
SELECT * FROM People WHERE ID = ?
You can understand that this will only retrieve one row as the ID is unique:
usersTableAdapters.UsersTableAdapter user = new usersTableAdapters.UsersTableAdapter();
users.UsersDataTable userDataTable = user.getUserInfo(id);
I have then stored the DataTable into a session:
HttpContext.Current.Session.Add("userDT", userDataTable);
Now I am trying to figure out, how would I get a specific column from the userDT in the session? To be more clear the firstname column?
First cast the session object as a datatable.
var tbl = ((DataTable)HttpContext.Current.Session["userDT"])
Then use it like a data table:
var col = tbl.Columns["firstName"];
You have to cast Session object to users.UsersDataTable:
users.UsersDataTable userDataTable = Session["userDT"] as users.UsersDataTable;
Try this,
DataView dv = new DataView((DataTable)HttpContext.Current.Session["userDT"]);
get the table just with column
DataTable dt = dv.ToTable(true, "firstName");
That consist of 2 columns: roomType and no rooms
So I want to get no rooms value from room type that i have.
In SQL its look like this:
SELECT no_rooms from table name where roomtype = 'deluxe'
Result: 2
How to access that in LINQ query and store that value as int datatype?
I only know this code
string[] tableName = table.AsEnumerable()
.Select(s => s.Field<string>("NoRooms"))
.ToArray<string>()
.Where(?idont_know_the_query));
var results = from myRow in table.AsEnumerable()
where myRow.Field<String>("roomtype ") == "deluxe"
select myRow;
Here is just another way of retriving the data rows, assuming that table in your example is a DataTable
string expression = string.Format("roomtype='{0}'","deluxe");
var rows = dt.Select(expression);
var strRoomNumber = rows.Select(r=>r.Field<string>("roomNumber")).FirstOrDefault();
int no_rooms;
int.TryParse(strRoomNumber,out no_rooms);
This will return you the first no of rooms for the first matching record
var NoOfRooms= tablename.where(x=>x.roomType=="deluxe").ToList();
int total = NoOfRooms.count();
I have a Datatable (Groups) designed like so
ColumnA|ColumnB
X|Apple
Y|Purple
X|Apple
X|Mango
I basically want to select from columna where it's X and get the disinct from ColumnB
This is what I have
var names = (from DataRow dr in Groups.Rows
orderby (string)dr["ColumnB"]
select (string)dr["ColumnB"]).Distinct();
This will give me distinct but it gives me Purple, and i dont want purple.
Thanks!
var names = (from DataRow dr in Groups.Rows
where dr["ColumnA"] == "X"
orderby (string)dr["ColumnB"]
select (string)dr["ColumnB"]).Distinct();
DataTable dt2 = dt1.Select("ColumnA = 'X'").CopyToDataTable().DefaultView.ToTable(true, "ColumnB");
So here we are selecting only the rows of data that you want, only rows where columnA is X. Then we choose only to see columnB, but with unique values only. Doing it in this order, you'll receive another datatable to play with. It'll only contain 1 column, columnB, and it'll only have unique/distinct values.
Enjoy.
If your return has more than one value and you want to Distinct the whole set by just one of the values you should use a custom IEqualityComparer.
var names = (from DataRow dr in Groups.Rows
where (string)dr["ColumnA"] == "X"
orderby (string)dr["ColumnB"]
select new {
ColumnA = (string)dr["ColumnA"],
ColumnB = (string)dr["ColumnB"]
}).Distinct(new MyCustomEqualityComparer());
edit: to include where clause
edit2: changed to custom IEqualityComparer
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();
I've been trying to find an answer online but I'm working with a DataTable and I want to filter all rows based on the column name in the table but I can't seem to get the filterExpression to work. Even though the syntax is wrong, this is basically what I want it to do....
DataRow[] row = sqlDT.Select(ColumnName = "Foo", "ASC", DataViewRowState.CurrentRows);
Thanks so much.
There are a few ways to accomplish this. I would suggest using LINQ to filter the rows:
sqlDT = sqlDT.AsEnumerable().Where(r => r.Field<string>("ColumnName") == "Foo").CopyToDataTable();
You can also use the Select method or the DefaultView.RowFilter property:
//select method
sqlDT = sqlDT.Select("ColumnName = 'Foo'").CopyToDataTable();
//row filter property
sqlDT.DefaultView.RowFilter = "ColumnName = 'Foo'";
sqlDT = sqlDT.DefaultView.ToTable();
EDIT
If you just want to filter out unneeded columns, use the DefaultView.ToTable() method:
sqlDT = sqlDT.DefaultView.ToTable(false, "Column1", "Column2", "Column3");
You are doing it in the wrong order,simply do it like this:
Datatable.rows(0//its count).item("column name");
You could loop it then with foreach and make a list
you should try something like this
DataRow[] row = sqlDT.Select("ColumnName =' " + Foo + " ' " , "ASC", DataViewRowState.CurrentRows);
I hope this work for you.