Getting metadata about dataset using lambda. Some examples.

This may not be a massive business requirement, but if you have a bunch of DataSets and you want (for some reason) extract information about them

then using Lambda and LINQ queries is one way to do it. I will let the code below speak for itself.

Sample code for meta data collection for DataSet:

    class Program

    {

        static void Main(string[] args)

        {

            // Create a demo dataset

            DataSet ourDataSet = CreateDataSet();

            // Display the DataTable info

            ShowTableCollectionInfo(ourDataSet.Tables);

        }

        private static void ShowTableCollectionInfo(DataTableCollection dtc)

        {

            IEnumerable<DataTable> tbls = dtc.Cast<DataTable>();

            int tblCount = dtc.Count;

            StringBuilder sb = new StringBuilder();

            // Which table(s) has the most rows?

            int maxRows = tbls.Max(x => x.Rows.Count);

            var maxRowsTableQuery = tbls.Where((x, max) => x.Rows.Count == maxRows);

            sb.AppendFormat("Table(s) with highest # of rows ({0}):", maxRows, tblCount);

            foreach (DataTable tbl in maxRowsTableQuery)

            {

                sb.AppendFormat("\n\t{0}", tbl.TableName);

            }

            // How many rows are there in total, i.e. sum of rows in all datatables?

            int totalRows = tbls.Sum(x => x.Rows.Count);

            sb.AppendFormat("\n\nTotal # of rows: {0} (in {1} tables)", totalRows, tblCount);

            // How many column on average.

            double columnAve = tbls.Average(x => x.Columns.Count);

            sb.AppendFormat("\n\nAverage # of cols: {0} (in {1} tables)", columnAve.ToString(), tblCount);

            //etc.

          

            // Show results

            Console.WriteLine(sb.ToString());

        }

        private static DataSet CreateDataSet()

        {

            DataTable dt_1 = new DataTable("DataTable_One");

            dt_1.Columns.Add(new DataColumn("T1_C1", typeof(int)));

            dt_1.Columns.Add(new DataColumn("T1_C2", typeof(int)));

            dt_1.Columns.Add(new DataColumn("T1_C3", typeof(int)));

            DataTable dt_2 = new DataTable("DataTable_Two");

            dt_2.Columns.Add(new DataColumn("T2_C1", typeof(int)));

            dt_2.Columns.Add(new DataColumn("T2_C2", typeof(int)));

            DataTable dt_3 = new DataTable("DataTable_Three");

            dt_3.Columns.Add(new DataColumn("T3_C1", typeof(int)));

            DataTable dt_4 = new DataTable("DataTable_Four");

            dt_4.Columns.Add(new DataColumn("T4_C1", typeof(int)));

            dt_4.Columns.Add(new DataColumn("T4_C2", typeof(int)));

            dt_4.Columns.Add(new DataColumn("T4_C3", typeof(int)));

            DataSet ds = new DataSet("OurDataSet");

            ds.Tables.Add(dt_1);

            ds.Tables.Add(dt_2);

            ds.Tables.Add(dt_3);

            ds.Tables.Add(dt_4);

            for (int i = 0; i < 10; i++)

            {

                dt_1.Rows.Add(new object[] { i, i + 5, i + 10 });

            }

            for (int i = 0; i < 5; i++)

            {

                dt_2.Rows.Add(new object[] { i, i * 5 });

            }

            for (int i = 0; i < 15; i++)

            {

                dt_3.Rows.Add(new object[] { i * 10 });

            }

            for (int i = 0; i < 5; i++)

            {

                dt_4.Rows.Add(new object[] { i, i + 100, i + 50 });

            }

            return ds;

        }

    }

Running this should output the following:

Table(s) with highest # of rows (15):

        DataTable_Three

Total # of rows: 35 (in 4 tables)

Average # of cols: 2,25 (in 4 tables)