last day (14 days later) » 

5:42 PM
0
A: how to pass custom query results to view in asp.net

ShyjuCurrently reservedCars is still a query expression of type IQueryable. It hasn't been evaluated/executed yet. So the type of reservedCars is different than the type your view is strongly typed to(IEnumerable<ReservedCar>). That is the reason you are getting the type mismatch error. Make sure you...

 
still getting error in controller. 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query. Answer is updated. Please check my Model class.
 
Do you have a class called ReserverCar ? How does it looks like ? Does it have the same proeprties as we used ?
 
check my model class in updated question. yeah it have all these properties.
 
How does the Car class looks like ? Do they have the same properties ?
Your code should work fine if your Car class has properties like Car_Id,Car_Description etc (as i mentioned in the answer). Make sure you are following the answer as t is. No types or anything. Verify it closely. The code in the answer should work fine 100 %
 
yes i checked closely and i have all these properties in Car model. but still getting following error. Additional information: The entity or complex type 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query.
 
5:42 PM
Just copied your class definition to a local project and used in a linq expresson projection and it worked totally fine !
 
its not working. i check each and every thing. Additional information: The entity or complex type 'FinalProject.Models.ReservedCar' cannot be constructed in a LINQ to Entities query. :-(
finaly i resolved my problem. we cant use model object in linq query. Read this issue. to resolve this problem that i was facing. stackoverflow.com/questions/12916080/…
im here
 
I see. Create a new view model and use that then
 
i resolved this issue by following code. because we cant use model object in linq query.
var reservedCars = (from c in db.Cars

join o in db.Orders on c.Car_Id equals o.Car_Id
join u in db.AspNetUsers on o.Id equals u.Id
where u.Id == 5
select new
{
Car_Id = c.Car_Id,
Car_Description = c.Car_Description,
Color = c.Color
}).AsEnumerable().Select(x => new Car
{
Car_Id = x.Car_Id,
Car_Description = x.Car_Description,
Color = x.Color
}).ToList();
Thanks for your time. Please update you answer so that i can accept your answer.
 
I updated the answer to use a view model.
This is the approach i do all the time.
So i never knew about that error
But you can also return c
where u.Id select c
and to ToList()
also
Make sure your view is strongly typed to whatever type you are returning
 

last day (14 days later) »