using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace DataTableExample { class Program { static void Main(string[] args) { DataTable dt = new DataTable("Student"); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Branch", typeof(string)); dt.Columns.Add("Age", typeof(int)); dt.Columns.Add("SocialMarks", typeof(int)); dt.Columns.Add("MathsMarks", typeof(int)); dt.Columns.Add("ScienceMarks", typeof(int)); dt.Columns.Add("TotalMarks", typeof(int)); dt.Columns.Add("AverageMarks", typeof(decimal)); List<Students> students = new List<Students>();//Students the type or namespace coulnt be found Console.WriteLine("Please enter a number to create students and start processing"); string[] classes = { "Semester-I", "Semester-II", "Semester-III", "Semester-IV" }; string[] Branches = { "Computers", "Electronics", "Civil", "Mechanics" }; Random cls = new Random(); Random brnc = new Random(); Random ag = new Random(); Random marks1 = new Random(); Random marks2 = new Random(); Random marks3 = new Random(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { DataRow dr = dt.NewRow(); dr["id"] = i; dr["Name"] = "Name" + i.ToString(); dr["Branch"] = Branches[brnc.Next(0, 3)]; int age = ag.Next(17, 22); int socialMarks = marks1.Next(50, 100); int mathsMarks = marks2.Next(50, 100); int scienceMarks = marks3.Next(50, 100); int totalMarks = socialMarks + mathsMarks + scienceMarks; dr["Age"] = age; dr["SocialMarks"] = socialMarks; dr["MathsMarks"] = mathsMarks; dr["ScienceMarks"] = scienceMarks; dr["TotalMarks"] = totalMarks; dr["AverageMarks"] = (float.Parse(dr["TotalMarks"].ToString()) / 3).ToString(); dt.Rows.Add(dr); } Console.WriteLine("from data table"); Console.WriteLine("ID | Name | Age| Branch | Socail | Science | Maths | Total Marks|Average"); foreach (DataRow dr in dt.Rows) { Console.WriteLine(dr["Id"] + "| " + dr["Name"] + "|" + dr["Age"] + "|" + dr["Branch"] + "|" + dr["SocialMarks"] + "|" + dr["ScienceMarks"] + "|" + dr["MathsMarks"] + "|" + dr["TotalMarks"] + "|" + dr["AverageMarks"]); } Console.WriteLine("list the count of students agewise"); var groupbyage = Student.GroupBy(s => s.Age).Select(lst => new { Age = lst.Key, Count = lst.Count() }); foreach (var ags in groupbyage.OrderBy(lst => lst.Age)) { Console.WriteLine("Age : " + ags.Age + " | Count: " + ags.Count); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)