使用 C# 访问数据集值
Accessing to a dataset value using C#
我有一个调用 Web 服务的应用程序,其中 GetDataSourceMappingTable 是一个数据结构 data set
。
我正在尝试提取特定列 (Users_Tests) 的值,这将给我强制参数,以便能够调用另一个 Web 服务 GetUser()
.
这里有数据集结构:
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>
我需要调用元素名称 "Caption"
并传递值 Users_Test
(其中 Users_Test 它是 table 中的一个值)以获得 SourceID e.g. "Data1"
到目前为止,这是我的代码:
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
每当我运行 GetUser()
方法中的DataSourceId 变量中的程序没有被正确传递。我得到一个数组 0 和 1。在 0 中我得到 Data1,在 1 中我得到 Users_tests.
我想得到例如"Data1"
如何只能获取Caption的值,而将SourceID
赋给GetUser()
方法?
我也希望能够有多个字幕,例如我们(Select("Caption = 'Users_test'");
和 Select("Caption = 'Users_test1'");
和 Select("Caption = 'Users_test3'");
这可能吗?
谢谢
DataTable.Select()
returns a DataRow[]
array, so you can use the Linq Select
方法将行投影到 Caption
的条目。下面的表达式获取你的caption对应的SourceID
值,如果没有找到returnsnull
,多次匹配抛出异常:
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.
如果您合理地期望 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)
{
}
原型fiddle.
更新
要 select 带有 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
我有一个调用 Web 服务的应用程序,其中 GetDataSourceMappingTable 是一个数据结构 data set
。
我正在尝试提取特定列 (Users_Tests) 的值,这将给我强制参数,以便能够调用另一个 Web 服务 GetUser()
.
这里有数据集结构:
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>
我需要调用元素名称 "Caption"
并传递值 Users_Test
(其中 Users_Test 它是 table 中的一个值)以获得 SourceID e.g. "Data1"
到目前为止,这是我的代码:
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
每当我运行 GetUser()
方法中的DataSourceId 变量中的程序没有被正确传递。我得到一个数组 0 和 1。在 0 中我得到 Data1,在 1 中我得到 Users_tests.
我想得到例如"Data1"
如何只能获取Caption的值,而将SourceID
赋给GetUser()
方法?
我也希望能够有多个字幕,例如我们(Select("Caption = 'Users_test'");
和 Select("Caption = 'Users_test1'");
和 Select("Caption = 'Users_test3'");
这可能吗?
谢谢
DataTable.Select()
returns a DataRow[]
array, so you can use the Linq Select
方法将行投影到 Caption
的条目。下面的表达式获取你的caption对应的SourceID
值,如果没有找到returnsnull
,多次匹配抛出异常:
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.
如果您合理地期望 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)
{
}
原型fiddle.
更新
要 select 带有 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