locked
accessing data in DATASET RRS feed

  • Question

  • HI,

    I can't find a sample code for accessing specific string data in dataset: supposed I want to access a string variable in a dataset - in row 2 and column 5.

    Thanks in advance,

    Dror.

    Saturday, May 5, 2007 4:00 PM

Answers

  • Something like this perhaps:

    Code Snippet

    DataTable table = new DataTable();
    table.Columns.Add("One", typeof(string));
    table.Columns.Add("Two", typeof(string));
    table.Columns.Add("Three", typeof(string));
    table.Columns.Add("Four", typeof(string));
    table.Columns.Add("Five", typeof(string));

    for (int i=0; i<10; i++)
        table.Rows.Add(new object[] { "1-" + i, "2-" + i, "3-" + i, "4-" + i, "5-" + i });

    // using LINQ to DataSet
    Console.WriteLine ( table.AsEnumerable().Skip(1).First().Field<string>(4));
    // traditional
    Console.WriteLine(table.Rows[1][4]);

     

     

    Sunday, May 6, 2007 10:29 AM

All replies

  • Something like this perhaps:

    Code Snippet

    DataTable table = new DataTable();
    table.Columns.Add("One", typeof(string));
    table.Columns.Add("Two", typeof(string));
    table.Columns.Add("Three", typeof(string));
    table.Columns.Add("Four", typeof(string));
    table.Columns.Add("Five", typeof(string));

    for (int i=0; i<10; i++)
        table.Rows.Add(new object[] { "1-" + i, "2-" + i, "3-" + i, "4-" + i, "5-" + i });

    // using LINQ to DataSet
    Console.WriteLine ( table.AsEnumerable().Skip(1).First().Field<string>(4));
    // traditional
    Console.WriteLine(table.Rows[1][4]);

     

     

    Sunday, May 6, 2007 10:29 AM
  • hi miha,

     

    thanks for your reply - but I need the code in VB (.NET), sorry for not mentioning it earlier...

     

     

    Sunday, May 6, 2007 12:37 PM
  • Sorry, but this code is easily convertible to VB.NET...
    Monday, May 7, 2007 9:30 AM
  • I posted an article about LINQ to Datasets a while back using the March 2006 CTP. I haven't had a chance to test it against against the Orcas Beta1 yet. Check it out at http://devauthority.com/blogs/jwooley/archive/2006/06/10/1276.aspx.

     

    Jim Wooley

    http://devauthority.com/blogs/jwooley

    http://linqinaction.net

    Monday, May 7, 2007 7:24 PM
  • Hi Miha,

     

    I hate to bother, but I really having hard time to donvert it - unsuccessfully....

    Thursday, May 10, 2007 9:37 PM
  • Here's the VB translation. There are a couple of tricks that need to be applied in the translations.

     

    Code Snippet

     Dim table As New DataTable
            table.Columns.Add("One", GetType(String))
            table.Columns.Add("Two", GetType(String))
            table.Columns.Add("Three", GetType(String))
            table.Columns.Add("Four", GetType(String))
            table.Columns.Add("Five", GetType(String))

            For i As Integer = 0 To 9
                table.Rows.Add(New Object() {"1-" + i.ToString, "2-" + i.ToString, "3-" + i.ToString, "4-" + i.ToString, "5-" + i.ToString})
            Next

            ' using LINQ to DataSet
            Console.WriteLine((From t In table.AsEnumerable Select t) _
                              .Skip(1).FirstOrDefault().Field(Of String)(4))

            '// traditional
            Console.WriteLine(table.Rows(1)(4))

     

    Note, in this case the Select t expression is not required. If you don't need to dynamically select the column, but can leverage the column name you could use the Bang notation in VB and not need to worry about the type conversion for the column as follows:

     

         

    Code Snippet
       Console.WriteLine((From t In table.AsEnumerable _
                               Select t!Five).Skip(1).First.ToString)

     

    It should get even easier in VB with beta 2 when the Skip method will be a valid query expression as well.

     

    Jim Wooley

    http://linqinaction.net

    http://devauthority.com/blogs/jwooley

    Friday, May 11, 2007 2:38 AM