Friday, March 15, 2013

JOIN CLAUSE in LINQ ENTITY

 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>();
        }
    }


 List<Books> books = Books.GetBooks();

            //SELECT Title from Books
            var booktitles = from b in books select b.Title;
           

            foreach (var title in booktitles)
                Label1.Text += String.Format("{0} <br />", title);

            //The Join clause:
            //query on both the tables using the join clause

            IEnumerable<Books> books1 = Books.GetBooks();
            IEnumerable<Salesdetails> sales =
                                Salesdetails.getsalesdetails();
            var booktitles1 = from b in books1
                             join s in sales
                             on b.ID equals s.ID
                             select new { Name = b.Title, Pages = s.pages };
            foreach (var title in booktitles1)
                Label2.Text += String.Format("{0} <br />", title);


            //The Where clause:
            //The 'where clause' allows adding some conditional filters to the query.

            var booktitles2 = from b in books
                             join s in sales
                             on b.ID equals s.ID
                             where s.pages > 500
                             select new { Name = b.Title, Pages = s.pages };

            foreach (var title in booktitles2)
                Label3.Text += String.Format("{0} <br />", title);

            //The Orderby and Orderbydescending clauses:
            //These clauses allow sorting the query results.

            var booktitles3 = from b in books
                             join s in sales
                             on b.ID equals s.ID
                             orderby b.Price
                             select new
                             {
                                 Name = b.Title,
                                 Pages = s.pages,
                                 Price = b.Price
                             };

            foreach (var title in booktitles3)
                Label4.Text += String.Format("{0} <br />", title);

            //The Let clause:
            //The let clause allows defining a variable and assigning it a value calculated from the data values.

            var booktitles4 = from b in books
                             join s in sales
                             on b.ID equals s.ID
                             let totalprofit = (b.Price * s.sales)
                             select new { Name = b.Title, TotalSale = totalprofit };
            foreach (var title in booktitles4)
                Label5.Text += String.Format("{0} <br />", title);



No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...