LINQ to SQL Select Query with
Alias Name
Before writing a LINQ to SQL
query, we first need to create a DataContext using the LINQ to SQL Classes
template, to know more on how to create the DataContext refer to the post LINQ to SQL Sample
Once the DataContext is created
we can query the Object model using LINQ queries, let us consider the Employee
table which has the following structure.
The below code will fetch the
columns from the Employee Table with the actual column names from
the Table
EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();
var emp = (from e in dbContext.Employees
select new { e.ID, e.Name, e.Phone });
In many situations we will need
to have custom (Alias) names to the Columns, in SQL Query we can achieve this
using the AS Clause
Now let us see how the same can
be achieved using a LINQ query.
EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();
var emp = (from
e in dbContext.Employees
select new {
select new {
EmpID = e.ID,
EmpName = e.Name,
EmpPhone = e.Phone
});
No comments:
Post a Comment