Friday, March 15, 2013

TABLES JOIN CLAUSE in LINQ ENTITY

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

namespace LinqSamples
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        TMSMASTEREntities1 SDC = new TMSMASTEREntities1();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TMSMASTERConnectionString"].ConnectionString.ToString());
                BindGrid();             

            }

        }
        private void BindGrid()
        {

            //var query = from d in SDC.Employees
            //            from l in SDC.Students.Where(loc => loc.StudentId == d.EmpID).DefaultIfEmpty()
            //            where d.Emp_Name.Contains("fg") || l.Name.Contains("fgh")
            //            select new
            //            {
            //                Employees = d,
            //                Students = l,
                          
            //            };

            //var results = (from r in query.AsEnumerable()
            //               select new
            //               {
            //                   Emp_Name = r.Employees.Emp_Name,
            //                   Emp_Address = r.Employees.Emp_Address,                              
            //                   StudentId = r.Students.StudentId,
            //                   Name = r.Students.Name,
            //                   Address = r.Students.Address,
            //                   BirthDate = r.Students.BirthDate,
            //                   Image = r.Students.Image,
            //                   Email = r.Students.Email,
            //                   Mobile = r.Students.Mobile,
            //                   Description = r.Students.Description
                             
            //               }).ToList();


            var results = (from d in SDC.Employees
                           join l in SDC.Students on d.EmpID equals l.StudentId                         
                           select new
                           {
                               Emp_Name =d.Emp_Name,
                               Emp_Address =d.Emp_Address,
                               StudentId = l.StudentId,
                               Name = l.Name,
                               Address = l.Address,
                               BirthDate = l.BirthDate,
                               Image = l.Image,
                               Email = l.Email,
                               Mobile = l.Mobile,
                               Description = l.Description
                           }).ToList();
           

            grid.DataSource = results;
            grid.DataBind();

        }

    }
}

No comments:

Using Authorization with Swagger in ASP.NET Core

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