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. Why can't this cast be done?

Why can't this cast be done?

Scheduled Pinned Locked Moved LINQ
questionlearningcsharplinqtesting
4 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    Mustafa Ismail Mustafa
    wrote on last edited by
    #1

    I don't have a programming problem, but I'd like to know why. I have something like this

    List a,b,c

    var d = a.Except(b);
    c.Clear()
    c = d.ToList();

    Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:

    c = a.Except(b);

    If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

    P D 2 Replies Last reply
    0
    • M Mustafa Ismail Mustafa

      I don't have a programming problem, but I'd like to know why. I have something like this

      List a,b,c

      var d = a.Except(b);
      c.Clear()
      c = d.ToList();

      Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:

      c = a.Except(b);

      If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

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

      You can't cast it as such because a.Except(b) returns the type System.Linq.Enumerable.ExceptIterator. You have two options here:

      c.AddRange(a.Except(b));

      or

      c = a.Except(b).ToList();

      "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

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        You can't cast it as such because a.Except(b) returns the type System.Linq.Enumerable.ExceptIterator. You have two options here:

        c.AddRange(a.Except(b));

        or

        c = a.Except(b).ToList();

        "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

        M Offline
        M Offline
        Mustafa Ismail Mustafa
        wrote on last edited by
        #3

        I ended up doing the second one, but it seems counter intuitive (to me at least) not to be able to do it directly. Meaning that the cast should happen behind the scenes.

        If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

        1 Reply Last reply
        0
        • M Mustafa Ismail Mustafa

          I don't have a programming problem, but I'd like to know why. I have something like this

          List a,b,c

          var d = a.Except(b);
          c.Clear()
          c = d.ToList();

          Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List as such:

          c = a.Except(b);

          If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          Except does not return a list, but an enumerator that will calculate the results on demand. This is how all LINQ methods work (well, those that return IEnumerable): the result isn't calculated until it is used. It would be strange to have an implicit cast that has a side effect (evaluating the query).

          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