Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Cast Error

Cast Error

Scheduled Pinned Locked Moved LINQ
helpcsharpasp-netlinq
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • L Lost User

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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 is

      TestModel.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.

      My blog | My articles | MoXAML PowerToys | Onyx

      L 1 Reply Last reply
      0
      • P Pete OHanlon

        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 is

        TestModel.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.

        My blog | My articles | MoXAML PowerToys | Onyx

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        thanks Pete for your effort but the line u write will only return one row and that statement must return many rows

        md_refay

        P 1 Reply Last reply
        0
        • L Lost User

          thanks Pete for your effort but the line u write will only return one row and that statement must return many rows

          md_refay

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          There are alternatives for multiple rows, such as ToList(); in place of 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.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups