Problems with selecting 2 column values in Linq to Entity?

You cannot use projection when you expect IQueryable.

Up vote 0 down vote favorite share g+ share fb share tw.

Hi I am trying to select the values of two columns which are second driver and price but I am getting error: Cannot implicitly convert type 'System.Linq. IQueryable' to 'System.Linq. IQueryable'.

An explicit conversion exists (are you missing a cast? ) Below is the code: public IQueryable GetSecondDriverOption(int eventID) { ApextrackdaysEntities entity = new ApextrackdaysEntities(); IQueryable SecondDriver = from p in entity. Events where p.

ID == eventID select new{ p. SecondDriver, p. SecondDriverPrice}; return SecondDriver; } Any help or suggestions will be appreciated thnx entity-framework linq-to-sql asp.

Net-mvc-2 linq-to-entities link|improve this question edited Jun 21 '11 at 10:22daniel. Herken800111 asked Jun 21 '11 at 9:25Mr A715423 85% accept rate.

You cannot use projection when you expect IQueryable where Event is your mapped type. You must either select Event : IQueryable SecondDriver = from p in entity. Events where p.

ID == eventID select p; Or you must create new type and project data to a new type: public class EventDto { public Driver SecondDriver { get; set; } public Price SecondDriverPrice { get; set; } } and redefine your method: public IQueryable GetSecondDriverOption(int eventID) { ApextrackdaysEntities entity = new ApextrackdaysEntities(); IQueryable SecondDriver = from p in entity. Events where p. ID == eventID select new EventDto { SecondDriver = p.

SecondDriver, SecondDriverPrice = p. SecondDriverPrice }; return SecondDriver; }.

The class EventDto which you have used , is it custom shaped View model class . – Mr A Jun 21 '11 at 9:51 Call it like you want. It is class which allows you selecting only two columns / properties and return them from the method.

– Ladislav Mrnka Jun 21 '11 at 10:29.

You cannot return anonymous objects. Try like this: public IQueryable GetSecondDriverOption(int eventID) { ApextrackdaysEntities entity = new ApextrackdaysEntities(); var seconDriver = from p in entity. Events where p.

ID == eventID; select p; return secondDriver; }.

But I want both the columns not just seconddriver – Mr A Jun 21 '11 at 9:35 so can I do something like secondDriver. Price , viceversa in controller by call this class – Mr A Jun 21 '11 at 9:37 @Muhammad Awais, you should use a view model, so that the controller queries the data and fills this view model which will be passed to the view. This view model will contain the properties required by your view.

– Darin Dimitrov Jun 21 '11 at 9:46 You mean custom shaped View model class..? – Mr A Jun 21 '11 at 9:50 @Muhammad Awais, that's exactly what I mean. – Darin Dimitrov Jun 21 '11 at 9:51.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions