如何读取特定的列值在数据集中 [英] How to read Specific colum values In dataset
本文介绍了如何读取特定的列值在数据集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以最佳方式从数据集中检索特定列值
我的数据集如
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想要检索所有城市来自数据集的名称
如何在不重复的情况下阅读这些内容
在此先感谢
i want to retrieve all the city names From The dataset
how can i read those without repetition
Thanks In Advance
推荐答案
您好
您可以使用dataview来获取您的特定栏目
Hi
you can use dataview to get your specific column
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
我会使用Linq,虽然它不那么容易 - 我找不到一步法...这些都没有经过测试或者甚至编译,它只是一个可能的方式的粗略概念
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
// 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();
注意:这是从我的MacBook输入的,我要小心复制并粘贴它 - 重新输入它更好,看起来MacBook已经输入了一些奇怪的引用,例如
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
这篇关于如何读取特定的列值在数据集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文