【问题标题】:Accessing to a dataset value using C#使用 C# 访问数据集值
【发布时间】:2015-09-30 17:21:45
【问题描述】:

我有一个调用 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 是该表中的一个值)以获取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。

我想得到例如“数据1”

我如何只能获取 Caption 的值,但将 SourceID 提供给 GetUser() 方法?

我也希望能够拥有多个像我们这样的字幕(Select("Caption = 'Users_test'");Select("Caption = 'Users_test1'");Select("Caption = 'Users_test3'");

这可能吗?

谢谢

【问题讨论】:

    标签: c# xml web-services dataset


    【解决方案1】:

    DataTable.Select() 返回一个DataRow[] 数组,因此您可以使用Linq Select 方法将行投影到Caption 的条目。以下表达式获取与您的标题对应的 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.
    

    如果您可以合理地预期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

    更新

    要使用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
    

    【讨论】:

    • 优秀的解决方案!非常感谢它工作完美(第一个)
    • 您能否告诉我是否有多个要添加的标题(例如 Users_Test1、Users_Test2 等)我该怎么做?谢谢
    • @Aarancibia - 我不确定你想从那条评论中得到什么。建议您可能想要编辑您的问题以提供您想要做什么的example。或者问另一个问题,因为这里的首选格式是one question per question
    • 对不起,你是对的,我很抱歉。我已经编辑了这个问题。它在最后一句话。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 2012-09-20
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多