DataTable Filtering in c sharp

Posted by Amatya under C# on 5/9/2015 | Points: 10 | Views : 395 | Status : [Member] | Replies : 5
I have a DataTable in which their is a column of age and person, I want to get the person age which is oldest. I think we can get with Select in DataTable. Suggest me as soon as possible.
With Regards Amatya(Adi)

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks



Responses

Posted by: Jayakumars on: 5/11/2015 [Member] [MVP] Bronze | Points: 25

Up
1
Down
//First Way
//=================
DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr[0] = 21;
dr[1] = "AAA";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 20;
dr[1] = "AAA";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 22;
dr[1] = "AAA";
dt.Rows.Add(dr);

dt.Select("your code here for oldest");


///Second Way
///=================
SqlConnection cn = new SqlConnection("data source=PCNAME;uid=userid;password=pwd;database=DBNAME");
ds = new DataSet("Orders");
SqlDataAdapter da = new SqlDataAdapter("SELECT min(age) FROM persons", cn);
da.TableMappings.Add("Table", "Orders");
da.Fill(ds);


Mark as Answer if its helpful to you

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jyoti25589 on: 5/10/2015 [Member] Starter | Points: 25

Up
0
Down
Hi,

I think Select will be a good option and can fulfill your requirement.
Following is the syntax:
datatable.Select(filterExp, sortExp, rowstatevalue).
This can be used to get the records as per filter expression and sort expression.

Thanks,

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 5/10/2015 [Member] Silver | Points: 25

Up
0
Down
Can you give an example @Jyoti

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 5/11/2015 [Member] Silver | Points: 25

Up
0
Down
Thanks Jyoti n JayaKumar.. I got ;)

Feel free to share informations 9731764134
mail Id ' adityagupta200@gmail.com
Thanks

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Another option.

dt.AsEnumerable().Select(dr=>dr["age"]).OrderbyDesc(o=>o).ToList();


--
Thanks & Regards,
RNA Team

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response