Friday, December 2

Simple Linq Query With Demo

Try this with your own,
                            
this is my code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default6 : System.Web.UI.Page
{
    public class Student
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
        public List<int> Scores { get; set; }

    }
    public class School
    {
        public string Last { get; set; }
        public int ID { get; set; }
    }

    static List<Student> students = new List<Student>
    {
    new Student{First="shibashish",Last="mohanty",ID=1,Scores=new List<int> {12,13,14,15,16}},
    new Student{First="shiba",Last="nohapatra",ID=2,Scores=new List<int> {112,113,114,115,116}},
    new Student{First="shibuna",Last="mohanty",ID=3,Scores=new List<int> {122,123,124,125,126}},
    new Student{First="ashish",Last="noharana",ID=4,Scores=new List<int> {132,133,134,135,136}},
    new Student{First="shibu",Last="mahanta",ID=5,Scores=new List<int> {142,143,144,145,146}}
    };
    static List<School> schoolData = new List<School>
    {
     new School{Last="mohanty",ID=2},
        new School{Last="sahho",ID=1},
        new School{Last="senapati",ID=4},
        new School{Last="panda",ID=5}
    };
    protected void Page_Load(object sender, EventArgs e)
    {
        //var schoolData = new School[]
        //{
        //new School{Last="mohanty",ID=12},
        //new School{Last="sahho",ID=13},
        //new School{Last="senapati",ID=14},
        //new School{Last="panda",ID=15}
        //};


        IEnumerable<Student> studentQuery =
     from student in students
     where student.Scores[0] > 90
     orderby student.Last ascending
     select student;

        foreach (Student shibuna in studentQuery)
        {
            Response.Write("<br> First Example:  " + shibuna.Last + " " + shibuna.First);
        }

        var studentQuery2 =
    from student in students
    group student by student.Last[0];
        foreach (var studentGroup in studentQuery2)
        {
            Response.Write(studentGroup.Key);
            foreach (Student student in studentGroup)
            {
                Response.Write("<br> Second Example  " + student.Last + " " + student.First);
            }
        }

       

        var innerJoinQuery =
       from stud in students
       join Schl in schoolData on stud.ID equals Schl.ID orderby stud.First ascending
       select new { LastName = stud.First, schoolLastName = Schl.Last };
        foreach (var Details in innerJoinQuery)
        {
            Response.Write("<br> Third Example " + Details.LastName + "   " + Details.schoolLastName );
        }
    }
}
You just copy it and paste in your aspx page and do some more for better practice
Thanks & Regards 
Shibashish mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty