Cast Error
-
Iam working in asp.net mcv soluation i have that error -- Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Boolean]' to type 'NerdDinner.Models.RSVP this message appear to me when executing that line of code -- my code OrdersViewModel TestModel = new OrdersViewModel() -- The Error Appear In That Line TestModel.rsvp = (NerdDinner.Models.RSVP) _db.RSVPs.Select(i => i.DinnerID == ID); -- public class OrdersViewModel { public Dinner dinner {get;set;} public RSVP rsvp { get; set; } } -- TestModel.rsvp must return list of rows so i need some help in solve that problem thanks
md_refay
-
Iam working in asp.net mcv soluation i have that error -- Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Boolean]' to type 'NerdDinner.Models.RSVP this message appear to me when executing that line of code -- my code OrdersViewModel TestModel = new OrdersViewModel() -- The Error Appear In That Line TestModel.rsvp = (NerdDinner.Models.RSVP) _db.RSVPs.Select(i => i.DinnerID == ID); -- public class OrdersViewModel { public Dinner dinner {get;set;} public RSVP rsvp { get; set; } } -- TestModel.rsvp must return list of rows so i need some help in solve that problem thanks
md_refay
The code
_db.RSVPs.Select(i => i.DinnerID == ID);
doesn't return a row (or rows) of data. LINQ uses a concept called deferred execution so that the evaluation doesn't occur until it is actually evaluated. This means that this line actually returns a queryable object, which has methods that can transform this into a result. What you need to add isTestModel.rsvp = (NerdDinner.Models.RSVP)_db.RSVPs.Select(i => i.DinnerID == ID).SingleOrDefault();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
The code
_db.RSVPs.Select(i => i.DinnerID == ID);
doesn't return a row (or rows) of data. LINQ uses a concept called deferred execution so that the evaluation doesn't occur until it is actually evaluated. This means that this line actually returns a queryable object, which has methods that can transform this into a result. What you need to add isTestModel.rsvp = (NerdDinner.Models.RSVP)_db.RSVPs.Select(i => i.DinnerID == ID).SingleOrDefault();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
thanks Pete for your effort but the line u write will only return one row and that statement must return many rows
md_refay
There are alternatives for multiple rows, such as
ToList();
in place ofSingleOrDefault();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.