none
C# Linq Table

    Question


  • Hello

          Is there a way through language linq filter the table's data as a function of size.
          For example a table with 1000 records only filter 250 first rows, or a 1 / 4 the size of the table. Another  example if i have 100,000 the i filter 25000 first rows. with any filter to columns.

    Thanks
    Tuesday, May 05, 2009 2:02 PM

Answers

  • Hi Ric,

    If the table has 1000 rows, then  Take<DataRow>(table.Rows.Count/4) will fetch you first 250 rows. This is .Net framework 3.5.

                IEnumerable<DataRow> filteredCollection = table.AsEnumerable().Take<DataRow>(table.Rows.Count/4).Where(n => n.Field<string>("ProductName") == "AAAAA1");

    Hope this helps!
    Jagadish Krishnan
    Wednesday, May 06, 2009 7:00 AM

All replies

  • Hi Ricardo,

               DataTable table = new DataTable();

                table.Columns.Add("ProductName");

                //Add ten rows
                for (int i = 0; i < 10; i++)
                {
                    table.Rows.Add(new object[] { string.Format("AAAAA{0}", i.ToString()) });
                }

                //Apply filter to first 5 rows
                IEnumerable<DataRow> filteredCollection = table.AsEnumerable().Take<DataRow>(5).Where(n => n.Field<string>("ProductName") == "AAAAA1");

    Hope this helps!

    Jagadish Krishnan

    Tuesday, May 05, 2009 2:55 PM

  • Hello,

       How modify this code for Filter the top 100 of col


    string

     

    col=Convert.ToString ( tabela_booleana.Columns[0]);

     tot= tabela_booleana.Rows.count;
     top= (tot/4);

    DataView view = tabela_booleana.AsDataView(); "TOP" + top + col ;  // or view.RowFilter = "TOP 1000"  + col ;

     

     

    view.RowFilter =

    dataGridViewDadosTranformados.DataSource = view;

    Tuesday, May 05, 2009 3:45 PM
  • The TSQL TOP command doesn't take a column as a parameter...

    What result set are you trying to get back?

    [)amien
    Tuesday, May 05, 2009 9:48 PM
    Moderator


  •    Hello,

            The results I need is the first rows the DataTable, for example if table as 1000 rows then i need the first 250 (1/4),

    Thanks
    Tuesday, May 05, 2009 11:56 PM
  • Hi Ric,

    If the table has 1000 rows, then  Take<DataRow>(table.Rows.Count/4) will fetch you first 250 rows. This is .Net framework 3.5.

                IEnumerable<DataRow> filteredCollection = table.AsEnumerable().Take<DataRow>(table.Rows.Count/4).Where(n => n.Field<string>("ProductName") == "AAAAA1");

    Hope this helps!
    Jagadish Krishnan
    Wednesday, May 06, 2009 7:00 AM