Search Chirayu Software Solutions Blog

Monday, August 27, 2012

Linq Example

 protected void Page_Load(object sender, EventArgs e)
    {
        List<Books> books = Books.GetBooks();

        var booktitles = from b in books select b.Title;

        foreach (var title in booktitles)
            lblbooks.Text += String.Format("{0} <br />", title);
      IEnumerable<Books> books = Books.GetBooks();
      IEnumerable<Salesdetails> sales =
                         Salesdetails.getsalesdetails();
       var booktitles = from b in books
                      join s in sales
                     on b.ID equals s.ID
                   select new { Name = b.Title, Pages = s.pages, Sales = s.sales };
       foreach (var title in booktitles)
          lblbooks.Text += String.Format("{0} <br />", title);
      GridView1.DataSource = booktitles;
        GridView1.DataBind();
    }
    public class Books
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public DateTime DateOfRelease { get; set; }

        public static List<Books> GetBooks()
        {
            List<Books> list = new List<Books>();
         
            list.Add(new Books
            {
                ID = "001",
                Title = "Programming in C#",
                Price = 634.76m,
                DateOfRelease = Convert.ToDateTime("2010-02-05")
            });

            list.Add(new Books
            {
                ID = "002",
                Title = "Learn Jave in 30 days",
                Price = 250.76m,
                DateOfRelease = Convert.ToDateTime("2011-08-15")
            });

            list.Add(new Books
            {
                ID = "003",
                Title = "Programming in ASP.Net 4.0",
                Price = 700.00m,
                DateOfRelease = Convert.ToDateTime("2011-02-05")
            });

            list.Add(new Books
            {
                ID = "004",
                Title = "VB.Net Made Easy",
                Price = 500.99m,
                DateOfRelease = Convert.ToDateTime("2011-12-31")
            });

            list.Add(new Books
            {
                ID = "005",
                Title = "Programming in C",
                Price = 314.76m,
                DateOfRelease = Convert.ToDateTime("2010-02-05")
            });

            list.Add(new Books
            {
                ID = "006",
                Title = "Programming in C++",
                Price = 456.76m,
                DateOfRelease = Convert.ToDateTime("2010-02-05")
            });

            list.Add(new Books
            {
                ID = "007",
                Title = "Datebase Developement",
                Price = 1000.76m,
                DateOfRelease = Convert.ToDateTime("2010-02-05")
            });
            return list;
        }
    }
        public class Salesdetails
        {
            public int sales { get; set; }
            public int pages { get; set; }
            public string ID { get; set; }

            public static IEnumerable<Salesdetails> getsalesdetails()
            {
                Salesdetails[] sd =
      {
      new Salesdetails { ID = "001", pages=678, sales = 110000},
      new Salesdetails { ID = "002", pages=789, sales = 60000},
      new Salesdetails { ID = "003", pages=456, sales = 40000},
      new Salesdetails { ID = "004", pages=900, sales = 80000},
      new Salesdetails { ID = "005", pages=456, sales = 90000},
      new Salesdetails { ID = "006", pages=870, sales = 50000},
      new Salesdetails { ID = "007", pages=675, sales = 40000},
      };
                return sd.OfType<Salesdetails>();
            }
        }
   
Post a Comment