C# 使用C访问数据集值
我有一个调用web服务的应用程序,其中GetDataSourceMappingTable是一个数据结构,它是一个数据集 我试图做的是提取某个列Users\u Tests的值,这将给我一个强制参数,以便能够调用另一个web服务GetUser 这里有数据集结构:C# 使用C访问数据集值,c#,xml,web-services,dataset,C#,Xml,Web Services,Dataset,我有一个调用web服务的应用程序,其中GetDataSourceMappingTable是一个数据结构,它是一个数据集 我试图做的是提取某个列Users\u Tests的值,这将给我一个强制参数,以便能够调用另一个web服务GetUser 这里有数据集结构: GetDataTableResponse xmlns="http://tempuri.org/"> <GetDataTableResult> <xs:schema id="N
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>
每当我在GetUser方法内的DataSourceId变量中运行程序时,都没有正确传递。我得到一个数组0和1。在0中,我获取数据1,在1中,我获取用户\u测试
我想得到例如数据1
我如何才能只获取标题的值,而将SourceID提供给GetUser方法
此外,我希望能够有多个字幕,如us SelectCaption='Users_test';然后选择caption='Users_test1';然后选择caption='Users_test3'
这可能吗
谢谢返回DataRow[]数组,因此您可以使用Linq方法将行投影到标题项。以下表达式获取与标题对应的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.
如果您可能合理地期望有多行标题为“Users\u 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)
{
}
原型
更新
要使用DataTable选择多个标题,请选择、使用或运算符:
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
极好的解决方案!非常感谢它工作得非常完美第一个你能告诉我我是否有更多的标题要添加到添加中,例如Users_Test1,Users_Test2,等等我怎么做?thanks@Aarancibia-我不确定你想从那条评论中得到什么。建议您可能需要编辑您的问题,以提供您想要做什么的详细信息。或者问另一个问题,因为这里的首选格式是。对不起,你是对的,我道歉。我已经编辑了这个问题。它在最后一句。再次感谢
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