Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace redditTest
- {
- public partial class Students : System.Web.UI.Page
- {
- public List<Student> testList = new List<Student>()
- {
- new Student("Steve", 5),
- new Student("Steve", 2),
- new Student("Steve", 4),
- new Student("Steve", 1),
- new Student("Steve", 5),
- };
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- GridView1.DataSource = testList;
- GridView1.DataBind();
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- //LINQ way
- var goodStudent = from c in testList
- where c.studentGrade <= 4
- select c;
- goodStudent.ToList();
- GridView1.DataSource = goodStudent;
- //GridView1.DataSource = goodList;
- GridView1.DataBind();
- }
- protected void Button3_Click(object sender, EventArgs e)
- {
- List<Student> badList = new List<Student>();
- foreach(Student s in testList)
- {
- if (s.studentGrade <= 4)
- {
- badList.Add(s);
- }
- }
- GridView1.DataSource = badList;
- GridView1.DataBind();
- }
- }
- public class Student
- {
- public string studentName { get; set; }
- public int studentGrade { get; set; }
- public Student(string name, int grade)
- {
- studentName = name;
- studentGrade = grade;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment