locked
Can't Add the DataRow to the List<DataRow> RRS feed

  • Question

  • A method which return the Ienumerable<DataRow>  Then it can't be added to the  List<DataRow> ;

    Func< string, IEnumerable<DataRow> > name = ( s ) =>   table.select( @"columnName = '"+s+"'");

    List<DataRow> dList ;

    dList.add( name(s) );

    Error : Object not reference to the set of instance



    Sunday, February 24, 2013 4:20 PM

Answers

  • Combining the previous answers, here is a possible solution.

    Func<string, IEnumerable<DataRow>> name = (s) => table.Select(@"columnName = '" + s + "'");
    List<DataRow> dList = new List<DataRow>();
    dList.AddRange(name(s));

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Mike Feng Monday, February 25, 2013 3:47 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:08 PM
    Monday, February 25, 2013 12:15 PM
  • Hello, i am not sure if i got your question but i think you need not to add Ienumerable to list..

    you can try following

    Func< string, IEnumerable<DataRow> > name = ( s ) =>   table.select( @"columnName = '"+s+"'");
    List<DataRow> dList ;
    dList = name.tolist<DataRow>();
    

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Proposed as answer by Mike Feng Monday, February 25, 2013 3:47 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:07 PM
    Sunday, February 24, 2013 4:33 PM

All replies

  • Hello, i am not sure if i got your question but i think you need not to add Ienumerable to list..

    you can try following

    Func< string, IEnumerable<DataRow> > name = ( s ) =>   table.select( @"columnName = '"+s+"'");
    List<DataRow> dList ;
    dList = name.tolist<DataRow>();
    

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Proposed as answer by Mike Feng Monday, February 25, 2013 3:47 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:07 PM
    Sunday, February 24, 2013 4:33 PM
  • Sure thing it fails, as you haven't initialized dList before calling method Add() on it. You have to create an instance of class (in your case, List<DataRow>) before doing something with it.

    List<DataRow> dList = new List<DataRow>();
    
    dList.Add( name(s) );

    Monday, February 25, 2013 3:45 AM
  • Call the AddRange method, not Add.
    Monday, February 25, 2013 10:39 AM
  • Combining the previous answers, here is a possible solution.

    Func<string, IEnumerable<DataRow>> name = (s) => table.Select(@"columnName = '" + s + "'");
    List<DataRow> dList = new List<DataRow>();
    dList.AddRange(name(s));

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Mike Feng Monday, February 25, 2013 3:47 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:08 PM
    Monday, February 25, 2013 12:15 PM
  • Func<string, IEnumerable<DataRow>> name = (s) => table.Select(@"columnName = '" + s + "'");
    List<DataRow> dList = new List<DataRow>();
    dList
    .AddRange(name(s));

    Kind regards, Brus

    Thursday, January 16, 2014 11:28 AM
  • Kind

       And what would be the best way for me to add multiple find (that are within a for) within a datarow?
      

       Creates a list of datarow?
       Or could roll the dice on a DataRowCollection?

    Wednesday, September 3, 2014 9:03 PM
  • Kind

     I mounted a smallsimpleexamplein order todemonstrate what I'mthinking of doing.

         How do I deletealldrFindx1,drFindx2,drFindx3anddrFindx4 datarow,and use the dr_Find_Row,which is a DataRowCollectionbecause I like to leave allfindwithin aDataRowCollection?

    drFindx1=tb_Table.Rows.Find(strFind_new)
    'dr_Find_Row.Add(tb_Table.Rows.Find (strFind_new))

             Dim tb_Table As DataTable
             Dim objColumn As DataColumn
             Dim obj_Data_Row As DataRow
    
             tb_Table = New DataTable("tbl_Table")
             objColumn = New DataColumn()
             With objColumn
                .ColumnName = "id_Tabela"
                .DataType = System.Type.GetType("System.Int32")
                .ReadOnly = False
                .Unique = True
                .AllowDBNull = False
             End With
             tb_Table.Columns.Add(objColumn)
    
             objColumn = New DataColumn()
             objColumn.DataType = GetType(System.String)
             objColumn.ColumnName = "LastName"
             objColumn.ReadOnly = False
             objColumn.Unique = True
             objColumn.AllowDBNull = False
             objColumn.MaxLength = 40
             tb_Table.Columns.Add(objColumn)
    
             objColumn = New DataColumn()
             objColumn.DataType = GetType(System.String)
             objColumn.ColumnName = "FirstName"
             objColumn.ReadOnly = False
             objColumn.Unique = True
             objColumn.AllowDBNull = False
             objColumn.MaxLength = 40
             tb_Table.Columns.Add(objColumn)
    
             obj_Data_Row = tb_Table.NewRow
             obj_Data_Row("id_Tabela") = 5
             obj_Data_Row("LastName") = "nomex a"
             obj_Data_Row("FirstName") = "nomey a"
             tb_Table.Rows.Add(obj_Data_Row)
    
             obj_Data_Row = tb_Table.NewRow
             obj_Data_Row("id_Tabela") = 6
             obj_Data_Row("LastName") = "nomex b"
             obj_Data_Row("FirstName") = "nomey b"
             tb_Table.Rows.Add(obj_Data_Row)
    
             obj_Data_Row = tb_Table.NewRow
             obj_Data_Row("id_Tabela") = 8
             obj_Data_Row("LastName") = "nomex c"
             obj_Data_Row("FirstName") = "nomey c"
             tb_Table.Rows.Add(obj_Data_Row)
    
             obj_Data_Row = tb_Table.NewRow
             obj_Data_Row("id_Tabela") = 9
             obj_Data_Row("LastName") = "nomex d"
             obj_Data_Row("FirstName") = "nomey d"
             tb_Table.Rows.Add(obj_Data_Row)
    
             obj_Data_Row = tb_Table.NewRow
             obj_Data_Row("id_Tabela") = 12
             obj_Data_Row("LastName") = "nomex e"
             obj_Data_Row("FirstName") = "nomey e"
             tb_Table.Rows.Add(obj_Data_Row)
    
             Dim dcKey(1) As DataColumn
             dcKey(0) = tb_Table.Columns("LastName")
             dcKey(1) = tb_Table.Columns("FirstName")
             tb_Table.PrimaryKey = dcKey
    
             Dim strFind(1) As String
             strFind(0) = "nomex d"
             strFind(1) = "nomey d"
             Dim drFind As DataRow
             drFind = tb_Table.Rows.Find(strFind)
             If drFind Is Nothing Then
                MessageBox.Show("No match")
             Else
                MessageBox.Show(CStr(drFind.Item("id_Tabela")))
             End If
    
             'new sub
             Dim strFind_new(1) As String
             Dim dr_Find_Row As DataRowCollection
    
             For id_Row_tb As Integer = 0 To 3
    
                strFind_new(0) = "nomex d"
                strFind_new(1) = "nomey d"
                Dim drFindx1 As DataRow
                drFindx1 = tb_Table.Rows.Find(strFind_new)
                'dr_Find_Row.Add(tb_Table.Rows.Find(strFind_new))
    
                strFind_new(0) = "nomex a"
                strFind_new(1) = "nomey a"
                Dim drFindx2 As DataRow
                drFindx2 = tb_Table.Rows.Find(strFind_new)
                'dr_Find_Row.Add(tb_Table.Rows.Find(strFind_new))
    
                strFind_new(0) = "nomex e"
                strFind_new(1) = "nomey e"
                Dim drFindx3 As DataRow
                drFindx3 = tb_Table.Rows.Find(strFind_new)
                'dr_Find_Row.Add(tb_Table.Rows.Find(strFind_new))
    
                strFind_new(0) = "nomex c"
                strFind_new(1) = "nomey c"
                Dim drFindx4 As DataRow
                drFindx4 = tb_Table.Rows.Find(strFind_new)
                'dr_Find_Row.Add(tb_Table.Rows.Find(strFind_new))
    
             Next
    
    

               
    Thursday, September 4, 2014 2:40 AM