Friday, 27 April 2012

LINQ QUERY

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<emp> x = new List<emp>()
        {
            new emp{ EID=1, NAME="siv", MGR=0, SALARY=5000,DID=1},
            new emp{ EID=2, NAME="sankar", MGR=1, SALARY=4000,DID=2},
              new emp{ EID=3, NAME="mahadev", MGR=2, SALARY=3000,DID=3},
               new emp{ EID=4, NAME="ram", MGR=1, SALARY=2000,DID=5}
        };
        List<dept> y = new List<dept>()
        {
            new dept{ DID=1, DNAME="A"},
            new dept{ DID=2, DNAME="B"},
            new dept{ DID=3, DNAME="C"},
            new dept{ DID=4, DNAME="D"}
        };
        List<salgrade> z = new List<salgrade>()
        {
             new salgrade{ LOWSAL=1, HISAL=1000, RANK=1},
              new salgrade{ LOWSAL=1001, HISAL=2000, RANK=2},
               new salgrade{ LOWSAL=2001, HISAL=3000, RANK=3},
               new salgrade{ LOWSAL=3001, HISAL=4000, RANK=4},
               new salgrade{ LOWSAL=4001, HISAL=5000, RANK=5}
        };
        var m = from a in x select a;
     var m = from a in x where a.SALARY>=3000 select a;
      var m = from a in x where a.SALARY==4000 && a.NAME=="sankar" select a;
    var m = from a in x where a.SALARY>=4000 || a.NAME=="sankar" select a;
    var m = from a in x where  a.NAME!="sankar" select a;
    var m = (from x in o.EMP5s select x.SAL).Distinct();
    var m = from a in x orderby a.SALARY ascending  select a;
     var m = from a in x group a by a.DID into b where b.Count() > 1 select new {b.Key,n=b.Count() };
      var m = from x in o.EMP5s group o.EMP5s by x.DID into y
                from z in o.DEPTs where z.DID==y.Key
                select new {DID=y.Key,z.DNAME, coun = y.Count() };   
     var m = from a in x where a.SALARY < x.Max(b => b.SALARY) select a;
     var m = from a in x where a.SALARY > x.Min(b => b.SALARY) select a;
         var m = from a in x select new {a.EID,a.NAME,Sumsallary=x.Sum(b=>b.SALARY) };
     var m=from a in x
                from b in y
                where a.DID == b.DID
                select new {a.EID,a.NAME,b.DID,b.DNAME};
    var m=from a in x
                from b in y
                from c in z
                where a.DID == b.DID && (a.SALARY >= c.LOWSAL && a.SALARY <= c.HISAL)
                select new {a.EID,a.NAME,a.SALARY,b.DNAME,c.RANK};
var m = from a in x
                from b in y
                where 1==1
                select new { a.EID, a.NAME, b.DNAME };
        var m=from a in x
                from b in y
                from c in z
                where a.DID == b.DID && (a.SALARY >= c.LOWSAL && a.SALARY <= c.HISAL)
                select new {a.EID,a.NAME,a.SALARY,b.DNAME,c.RANK};
     var m =from a in x
               join b in y on a.DID equals b.DID into c
               from b in c.DefaultIfEmpty()
               select new {a.EID,a.NAME,DNAME=(b==null?null:b.DNAME)};
    var  m=from a in y
                join b in x
                on a.DID equals b.DID
                into c
                from b in c.DefaultIfEmpty()
                where b == null
                select a;
    var m=from a in x
              join b in x
              on a.MGR equals b.EID
              select new { mark = a.NAME + " is working under " + b.NAME };
     var m = from a in x
                join b in x
                on a.MGR equals b.EID
                into c from b in c.DefaultIfEmpty()
                select new { mark =  a.NAME + " is working under " + (b==null?null :b.NAME)};
    var m = from a in x
                let b = (a.EID == 1 ? "siv" : a.EID == 2 ? "sankar" : "mahadev")
                select new {a.EID,a.NAME,b };
        gv.DataSource = m;
        gv.DataBind();
    }
    class emp
    {
        public int EID
        {
            get;
            set;
        }
        public string NAME
        {
            get;
            set;
        }
        public int MGR
        {
            get;
            set;
        }
        public decimal SALARY
        {
            get;
            set;
        }
        public int DID
        {
            get;
            set;
        }
    }
    class dept
    {
        public int DID
        {
            get;
            set;
        }
        public string DNAME
        {
            get;
            set;
        }
    }
    class salgrade
    {
        public decimal LOWSAL
        {
            get;
            set;
        }
        public decimal HISAL
        {
            get;
            set;
        }
        public int RANK
        {
            get;
            set;
        }
    }
}

No comments:

Post a Comment