A arancibia
A arancibia

Reputation: 281

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.

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().

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 e.g. "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. I get an array 0 and 1. In 0 I get Data1 and in 1 I get Users_tests.

I am suppose to get e.g. "Data1"

How I can only get the value of the Caption but give the SourceID to the GetUser() method?

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'");

Is this possible?

Thanks

Upvotes: 1

Views: 130

Answers (1)

dbc
dbc

Reputation: 116785

DataTable.Select() returns a DataRow[] array, so you can use the Linq Select method to project the rows to the entry for Caption. The following expression gets the SourceID value corresponding to your caption, returns null if not found, and throws an exception on multiple matches:

    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:

    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)
    {
    }

Prototype fiddle.

Update

To select multiple captions with DataTable.Select(), use the OR operator:

            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

Upvotes: 1

Related Questions