Saturday, 3 October 2015

datatable concept

  DataTable dt = new DataTable();


        private void button1_Click(object sender, EventArgs e)
        {
            dt.Columns.Add("SSN", typeof(string));
            dt.Columns.Add("NAME", typeof(string));
            dt.Columns.Add("ADDR", typeof(string));
            dt.Columns.Add("AGE", typeof(int));
            DataColumn[] KEY = new DataColumn[1];
            KEY[0] = dt.Columns[0];
            dt.PrimaryKey = KEY;
            dt.Rows.Add("203456843", "BHARTIJILEDAR", "13 Main Ct, Newyork, NY", 27);
            dt.Rows.Add("203456877", "SAM", "13 Main Ct, Newyork, NY", 25);
            dt.Rows.Add("203456878", "Elan", "14 Main Street, Newyork, NY", 35);
            dt.Rows.Add("203456879", "Smith", "12 Main Street, Newyork, NY", 45);
            dt.Rows.Add("203456880", "SAM", "345 Main Ave, Dayton, OH", 55);
            dt.Rows.Add("203456881", "Sue", "32 Cranbrook Rd, Newyork, NY", 65);
            dt.Rows.Add("203456882", "Winston", "1208 Alex St, Newyork, NY", 65);
            dt.Rows.Add("203456883", "Mac", "126 Province Ave, Baltimore, NY", 85);
            dt.Rows.Add("203456884", "SAM", "126 Province Ave, Baltimore, NY", 95);
            //RETREAVING THE DATA HAVING NAME NOT BE SAME
            foreach (DataRow o in dt.Select("NAME <> 'SAM'"))
            {
                //MessageBox.Show("\t" + DR["SSN"] + "\t" + DR["NAME"] + "\t" + DR["ADDR"] + "\t" + DR["AGE"]);
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + "\t" + o["NAME"] + "\t" + "\t" + o["ADDR"] + "\t" + o["AGE"]);

            }
            listBox1.Items.Add(" CHECKING THE AGE GREATER THEN 60");
            foreach (DataRow o in dt.Select("AGE>60").Take(2))
            {
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + "\t" + o["NAME"] + "\t" + "\t" + o["ADDR"] + "\t" + o["AGE"]);

            }
         
            listBox1.Items.Add(" AGE IN DECENDING ORDER");

            foreach (DataRow o in dt.Select( " ", "AGE DESC"))
            {
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + "\t" + o["NAME"] + "\t" + "\t" + o["ADDR"] + "\t" + o["AGE"]);

            }
            listBox1.Items.Add("name ascending order");
            foreach (DataRow o in dt.Select(" ", "NAME ASC"))
            { listBox1.Items.Add("\t" + o["SSN"] + "\t" + "\t" + o["NAME"] + "\t" + "\t" + o["ADDR"] + "\t" + o["AGE"]);

            }
            // Console.WriteLine(" The average of all the person's age is: " + avgAge);
            listBox1.Items.Add(" getting the name of most age person in the list");
            DataRow MOSTAGEDPERSON = dt.Select("", "AGE ASC").Last();
            {
                listBox1.Items.Add("\t" + MOSTAGEDPERSON["SSN"] + "\t" + "\t" + MOSTAGEDPERSON["NAME"] + "\t" + "\t" + MOSTAGEDPERSON["ADDR"] + "\t" + MOSTAGEDPERSON["AGE"]);


            }
           
            listBox1.Items.Add(" PERSON HAVING AGE GREATER THEN 60");
            foreach (DataRow o in dt.Select().SkipWhile(m => (int)m.ItemArray[3] < 60))
            {
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + "\t" + o["NAME"] + "\t" + "\t" + o["ADDR"] + "\t" + o["AGE"]);

            }

         
           listBox1.Items.Add(" Displaying the persons until we find a person with name starts with other than 'S'");
            foreach (DataRow o in dt.Select().Where(m => m.ItemArray[1].ToString().StartsWith("S")))
            {
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + o["NAME"] + "\t" + o["ADDR"] + "\t" + o["AGE"]);
            }

         
            listBox1.Items.Add("\n Checking all the persons have SSN or not ...");
            if (dt.Select().All(m => m.ItemArray[0] != null))
            {
                listBox1.Items.Add("\t No person is found without SSN");
            }

            Console.WriteLine("\n-----------------------------------------------------------------------------");
            listBox1.Items.Add("\n Finding the person whose SSN = 203456876 in the list");
            foreach (DataRow o in dt.Select("SSN = '203456876'"))
            {
                listBox1.Items.Add("\t" + o["SSN"] + "\t" + o["NAME"] + "\t" + o["ADDR"] + "\t" + o["AGE"]);
            }
            Console.WriteLine("\n-----------------------------------------------------------------------------");
            Console.Read();
            listBox1.Items.Add("HECKING  THE AGE BETWEEN 13  AND 19  WCHICH IS CALLED TEEN AGE");
            if (dt.Select("AGE >= 13 AND AGE <= 19").Any())
            {
                MessageBox.Show("yeS WE HAVE TEEN AGERS IN LIST");
            }
            else
            {
                MessageBox.Show("NO, WE nO  HAVE TEEN AGERS IN LIST");

            }
            Console.WriteLine("\n-----------------------------------------------------------------------------");
            Console.WriteLine("\n Checking whether a person having name 'SAM' exists or not...");
            if (dt.Select().Any(m => m.ItemArray[1].ToString() == "SAM"))
            {
                MessageBox.Show("Person having the NAme  Sam");
            }
            Console.WriteLine("\n Getting Average of all the person's age...");
            double Sumage = dt.Select("").Average(m => (int)m.ItemArray[3]);
            // double avgAge = dt.Select("").Average(m => (int)m.ItemArray[3]);
            MessageBox.Show("The Sum Of All Person Age Is" + Sumage);
         
        }


        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

No comments:

Post a Comment