Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to retrieve the specific column values From dataset in best way

my dataset like
Country  State     City         PinCode
India	Andhra	   Vijayawada	520005
India	Andhra	   Rajamundry	533101
India	Andhra	   Vijayawada	520001
India	Telangan   Hyderabad	500018
India	Kerala	   Palakkad	    678001
India	Kerala	   Kottayam	    686001
India	Kerala	   Kollam	    691001
India	Telangana  Madhira	    507203
India	Telangana  Hyderabad	500033
India	Telangana  Warangal	    506002
India	Telangana  Khammam	    507111
India	Telangana  Hyderabad	500081
India	Karnataka  Banglore	    560005
India	Karnataka  Manglore	    575001
India	Karnataka  Mysore	    570001
India	Karnataka  Banglore	    560001
India	Gujarat	   Surat	    395003
India	Gujarat	   Vadodara	    391110
India	Gujarat	   Rajakot	    360001


i want to retrieve all the city names From The dataset
how can i read those without repetition

Thanks In Advance
Posted
Comments
User-11630313 30-Dec-15 4:46am    
why do you want to read the particular column values?are you trying to fill dropdown?

Hi

you can use dataview to get your specific column

C#
DataView view = new DataView(dataset.Tables[0]);
DataTable cityColumn = view.ToTable(true, "City");
DataRow[] myRows = cityColumn.Select();


Now you can iterate your rows
Hope this helps

Thanks
Ankit
 
Share this answer
 
I would have used Linq, although its not 'that easy' - I couldnt find a single step method .. none of this has been tested or even compiled, its just a rough idea of a possible way

C#
// Given :-
// a DataSet ds
// a Table In The DataSet "MyTable"
// a Column Within the Table With The Column Name ‘City’

DataTable myDataTable = ds.Tables["MyTable"];

// Build a Linq Query
// One Could Include Other Filter Conditions Here 
IEnumerable<DataRow> query =
    from myDataRow in myDataTable.AsEnumerable()
    select myDataRow;

// A List To Hold ALL the City Values (We filter this later)
List<String> Cities = New List<String>();

// This Executes The Linq Query - In This Case We just capture 
// The Value of the City Column 
foreach (DataRow dr in query)
{
    Cities.Add(dr.Field<string>("City"));
}

// Roughly the end condition 
List<String> filteredCityList = Cities.Distinct();


NB : This was entered from my MacBook, I'd be careful of copying and pasting it - retype it is better, it looks like the MacBook has entered some strange quotes for example
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900