[英]Accessing to a dataset value using C#
I have a application that calls a web service where GetDataSourceMappingTable its a data structure its a data set
. 我有一个调用Web服务的应用程序,其中GetDataSourceMappingTable是它的数据结构,是
data set
。
What I am trying to do its to extract the value of a certain column (Users_Tests) which will give me the mandatory parameter to be able to call another web service GetUser()
. 我正在尝试执行的操作是提取某个列(Users_Tests)的值,这将为我提供强制性参数,以便能够调用另一个Web服务
GetUser()
。
Here you have data set structure: 这里您具有数据集结构:
GetDataTableResponse xmlns="http://tempuri.org/">
<GetDataTableResult>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="SourceID" type="xs:string" minOccurs="0"/>
<xs:element name="Caption" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I need to call the element name "Caption"
and pass the value Users_Test
(where Users_Test its a value inside that table) to get the SourceID eg "Data1"
我需要调用元素名称
"Caption"
并传递值Users_Test
(其中Users_Test其在该表内的值)以获取SourceID eg "Data1"
Here is my code so far: 到目前为止,这是我的代码:
var ds = proxy.GetDataTable();
var DataSourceId = ds.Tables["Table"].Select("Caption = 'Users_test'");
UserData[] userDataId = client.GetUser(ref apiKey, ref message, DataSourceId.ToString()); //GetUserData need the sourceID e.g. Data1 to be able to be used
Whenever I run the program in the DataSourceId variable inside GetUser()
method is not being passed correctly. 每当我在
GetUser()
方法内的DataSourceId变量中运行程序时,都无法正确传递该程序。 I get an array 0 and 1. In 0 I get Data1 and in 1 I get Users_tests. 我得到一个数组0和1。在0中得到Data1,在1中得到Users_tests。
I am suppose to get eg "Data1" 我想得到例如“ Data1”
How I can only get the value of the Caption but give the SourceID
to the GetUser()
method? 如何仅获取Caption的值,但如何将
SourceID
赋予GetUser()
方法?
As well I would like to be able to have multiple captions such us ( Select("Caption = 'Users_test'");
and Select("Caption = 'Users_test1'");
and Select("Caption = 'Users_test3'");
同样,我希望能够有多个字幕,例如我们(
Select("Caption = 'Users_test'");
和Select("Caption = 'Users_test1'");
和Select("Caption = 'Users_test3'");
Is this possible? 这可能吗?
Thanks 谢谢
DataTable.Select()
returns a DataRow[]
array, so you can use the Linq Select
method to project the rows to the entry for Caption
. DataTable.Select()
返回一个DataRow[]
数组,因此您可以使用Linq Select
方法将行投影到Caption
的条目。 The following expression gets the SourceID
value corresponding to your caption, returns null
if not found, and throws an exception on multiple matches: 以下表达式获取与您的标题对应的
SourceID
值,如果未找到则返回null
,并在多个匹配项上引发异常:
var DataSourceId = ds.Tables["Table"]
.Select("Caption = 'Users_test'") // Find rows with Caption = 'Users_test'
.Select(r => r["SourceID"]) // Project to the value of SourceId
.Where(s => s != DBNull.Value) // Filter DBNull (might occur when the SourceID cell is missing
.Select(s => s.ToString()) // Project to string value
.SingleOrDefault(); // null if no matches, throw an exception if more than one match.
If you might reasonably expect more than one row with Caption = 'Users_test'
, you can loop through them all with a foreach
statement: 如果您合理地期望
Caption = 'Users_test'
超过一行,则可以使用foreach
语句遍历它们:
var query = ds.Tables["Table"]
.Select("Caption = 'Users_test'") // Find rows with Caption = 'Users_test'
.Select(r => r["SourceID"]) // Project to the value of SourceId
.Where(s => s != DBNull.Value) // Filter DBNull (might occur when the SourceID cell is missing
.Select(s => s.ToString()); // Project to string value
foreach (var DataSourceId in query)
{
}
Update 更新
To select multiple captions with DataTable.Select()
, use the OR
operator: 要使用
DataTable.Select()
选择多个字幕,请使用OR
运算符:
var query = ds.Tables["Table"]
.Select("Caption = 'Users_test' OR Caption = 'Users_test3'") // Find rows any of several captions
.Select(r => r["SourceID"]) // Project to the value of SourceId
.Where(s => s != DBNull.Value) // Filter DBNull (might occur when the SourceID cell is missing
.Select(s => s.ToString()); // Project to string value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.