Here is my ViewModel
:
public class CarViewModel
{
public string Registration { get; set; }
public string ModelName { get; set; }
public string Colour { get; set; }
public DateTime Year { get; set; }
}
Here is my code
[HttpGet]
public ActionResult GetAllCars()
{
CarRentalEntities entities = new CarRentalEntities();
List<CarViewModel> carsViewModel = new List<CarViewModel>();
var cars = entities.GetAllCars();
// Is it possible to convert cars to carsViewModel Collection and
//return it to the view ????????????????
return View(carsViewModel);
}
Here is my SQL stored procedure which is called by Entity Framework
Create Procedure GetAllCars
as
(
Select
c.registration, m.model_name, c.colour, m.model_year
from
Cars c
inner join
models m on c.model_id = m.model_id
)
In my Entity Framework code all I want to do is convert the collection returned by the stored procedure GetAllCars
of type System.Data.Entity.Core.Object.ObjectResults<GetAllCars_Result>
into collection of type
List<CarViewModel>
.
How can this be achieved? I couldn't find proper tutorial or articles.
Thank you
[HttpGet]
public ActionResult GetAllCars()
{
CarRentalEntities entities = new CarRentalEntities();
var cars = entities.GetAllCars();
List<CarViewModel> carsViewModel = cars.Select(c=> new CarViewModel
{
Registration = c.Registration,
// and so on
}).ToList();
return View(carsViewModel);
}