Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Created March 21, 2014 02:53
Show Gist options
  • Save RyanABailey/9678550 to your computer and use it in GitHub Desktop.
Save RyanABailey/9678550 to your computer and use it in GitHub Desktop.
Entity Framework wrapper to return custom objects using LINQ
public static List<Employee> GetEmployessFromDepartment(int departmentID)
{
var searchResults = new List<Employee>();
using (var entities = new EFEntities())
{
try
{
searchResults = (from a in entities.SelectEmployeeByDepartment(departmentID)
select (new Employee()
{
FirstName = a.first_name,
SurName = a.surname,
Title = a.job_title,
Department = a.department_description,
Phone = a.phone_number,
Email = a.email_address
}))
.ToList();
}
catch (Exception ex)
{
Log.Error(ex);
throw;
}
}
return searchResults;
}
public static Employee GetEmployeeFromID(int employeeID)
{
var result = new Employee();
using (var entities = new EFEntities())
{
try
{
result = (from a in entities.SelectEmployee(employeeID)
select new Employee
{
FirstName = a.first_name,
SurName = a.surname,
Title = a.job_title,
Department = a.department_description,
Phone = a.phone_number,
Email = a.email_address
})
.SingleOrDefault();
}
catch (Exception ex)
{
Log.Error(ex);
throw;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment