Pages

Google Analytics Alternative

Search This Blog

Thursday, October 25, 2012

EntityFramework inner join

Column concatenation using Linq to EntityFramework.
here we have one simple example where there are two tables.
one
Employee with following fileds

CREATE TABLE [dbo].[empMaster](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Age] [int] NULL,
[IsActive] [bit] NULL CONSTRAINT [DF_empMaster_IsActive]  DEFAULT ((1)),
[CreatedDate] [datetime] NULL CONSTRAINT [DF_empMaster_CreatedDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_empMaster] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


and another table is with

CREATE TABLE [dbo].[employee](
[id] [int] IDENTITY(1,1) NOT NULL,
[Salary] [int] NULL,
 CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]







Now i want to get name of employee and salary of that employee.
so i used innerjoin like this

select em.id,em.name,e.salary
from empMaster em
inner join employee e
on em.id=e.id
Result of inner join.


but in Linq To EntityFrame work it works like this.

   var outputvalue = (from emp in obj.empMasters 
                                      join e in obj.employees on emp.Id equals e.id
                                     select new {
                                        emp.Name,e.Salary,emp.Id  
                                     }).ToList().Select(q =>new {Id = q.Id,Name = q.Name,dummyValue = q.Id.ToString() + " " + 
                                         q.Name.ToString()});

Thanks



No comments: