Easy way* to cast an object in a LINQ query? Methinks not*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    Easy way* to cast an object in a LINQ query? Methinks not*

    Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell
    (this book is amazing, you must get it if you have to buy but one C#
    book) as well as Appendix A of Jon Skeet's book, I am going through
    some LINQ queries. But how to cast? (

    See the below, modified from somebody else's code.

    The problem is the query 'stops' (throws a cast exception) at "3", and
    never gets to "violet".

    How to prevent this?

    Pseudocode OK, just a hint is all I need, but more of course is
    better.

    RL

    *PS--by "easy way to cast" I mean something very short, that can work
    at runtime, not something like a CASE/SWITCH statement that lists all
    possibilities ahead of time, but, I'll take all answers.

    //


    List<objectword s = new List<object{ "green", "blue", 3, "violet",
    5 };

    // Cast the objects in the list to type 'string'
    // and project the first two letters of each string.

    try
    {
    IEnumerable<str ingquery =
    words.AsQueryab le().Cast<strin g>().Select(st r =str.Substring( 0, 2));

    foreach (string s in query)
    Console.WriteLi ne(s);
    }

    catch (ArgumentExcept ion ex)
    {
    Console.WriteLi ne(ex);
    }
    catch (InvalidCastExc eption ex2)
    {
    Console.WriteLi ne("invalid cast!" + ex2);
    }

    /* This code *should* produce the following output, but
    it breaks out of the query when it gets to '3' above and throws an
    InvalidCastExce ption--how to get it to complete the query until the
    end?

    gr
    bl
    vi
    */

    RL
  • Family Tree Mike

    #2
    Re: Easy way* to cast an object in a LINQ query? Methinks not*

    I suggest a change your critical line to:

    IEnumerable<str ingquery =
    words.AsQueryab le().Cast<strin g>().Select(st r =str.Substring( 0,
    Math.Min(str.Le ngth, 2)));

    "raylopez99 " <raylopez99@yah oo.comwrote in message
    news:c6b1273e-51ba-4e80-8f31-54d7826f7976@e3 9g2000hsf.googl egroups.com...
    Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell
    (this book is amazing, you must get it if you have to buy but one C#
    book) as well as Appendix A of Jon Skeet's book, I am going through
    some LINQ queries. But how to cast? (
    >
    See the below, modified from somebody else's code.
    >
    The problem is the query 'stops' (throws a cast exception) at "3", and
    never gets to "violet".
    >
    How to prevent this?
    >
    Pseudocode OK, just a hint is all I need, but more of course is
    better.
    >
    RL
    >
    *PS--by "easy way to cast" I mean something very short, that can work
    at runtime, not something like a CASE/SWITCH statement that lists all
    possibilities ahead of time, but, I'll take all answers.
    >
    //
    >
    >
    List<objectword s = new List<object{ "green", "blue", 3, "violet",
    5 };
    >
    // Cast the objects in the list to type 'string'
    // and project the first two letters of each string.
    >
    try
    {
    IEnumerable<str ingquery =
    words.AsQueryab le().Cast<strin g>().Select(st r =str.Substring( 0, 2));
    >
    foreach (string s in query)
    Console.WriteLi ne(s);
    }
    >
    catch (ArgumentExcept ion ex)
    {
    Console.WriteLi ne(ex);
    }
    catch (InvalidCastExc eption ex2)
    {
    Console.WriteLi ne("invalid cast!" + ex2);
    }
    >
    /* This code *should* produce the following output, but
    it breaks out of the query when it gets to '3' above and throws an
    InvalidCastExce ption--how to get it to complete the query until the
    end?
    >
    gr
    bl
    vi
    */
    >
    RL

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Easy way* to cast an object in a LINQ query? Methinks not*

      raylopez99 <raylopez99@yah oo.comwrote:
      Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell
      (this book is amazing, you must get it if you have to buy but one C#
      book) as well as Appendix A of Jon Skeet's book, I am going through
      some LINQ queries. But how to cast? (
      >
      See the below, modified from somebody else's code.
      >
      The problem is the query 'stops' (throws a cast exception) at "3", and
      never gets to "violet".
      >
      How to prevent this?
      See the "OfType" method.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Family Tree Mike

        #4
        Re: Easy way* to cast an object in a LINQ query? Methinks not*

        By the way, the reason I said this is that I don't get an invalid cast, but
        rather
        an index out of range error...


        "Family Tree Mike" <FamilyTreeMike @ThisOldHouse.c omwrote in message
        news:uO0RRZPHJH A.3640@TK2MSFTN GP04.phx.gbl...
        >I suggest a change your critical line to:
        >
        IEnumerable<str ingquery =
        words.AsQueryab le().Cast<strin g>().Select(st r =str.Substring( 0,
        Math.Min(str.Le ngth, 2)));
        >
        "raylopez99 " <raylopez99@yah oo.comwrote in message

        Comment

        • raylopez99

          #5
          Re: Easy way* to cast an object in a LINQ query? Why yes, yes thereis...



          Family Tree Mike wrote:
          I suggest a change your critical line to:
          >
          IEnumerable<str ingquery =
          words.AsQueryab le().Cast<strin g>().Select(st r =str.Substring( 0,
          Math.Min(str.Le ngth, 2)));
          FTM--that didn't do anything important (or so it seems, but I left it
          in anyway, figuring you must be guarding against something), i.e., you
          still got the exception thrown. What was it supposed to guard
          against?

          Anyway, I took Jon Skeet's idea to mind, and here is what worked. I
          have a feeling the extra ".Cast<>" below is redundant...but don't see
          a problem leaving it in.

          RL

          //this worked...add-- OfType<string>( ). in the middle as shown

          IEnumerable<str ingquery =
          words.AsQueryab le().OfType<str ing>().Cast<str ing>().Select(s tr =>
          str.Substring(0 ,Math.Min(str.L ength, 2)));

          Comment

          • raylopez99

            #6
            Re: Easy way* to cast an object in a LINQ query? Methinks not*

            I see...index out of range would be if you did not put an index that's
            within the string length...makes sense... so ignore my question to you
            in the prior post.

            Thank you.

            Family Tree Mike wrote:
            By the way, the reason I said this is that I don't get an invalid cast, but
            rather
            an index out of range error...
            >

            Comment

            • Family Tree Mike

              #7
              Re: Easy way* to cast an object in a LINQ query? Methinks not*

              Just so we are clear, now I get no exceptions within this code, and the
              output is:

              gr
              bl
              3
              vi
              5

              "raylopez99 " <raylopez99@yah oo.comwrote in message
              news:248a058d-030f-445e-bc46-fed2b99ab9bf@73 g2000hsx.google groups.com...
              >I see...index out of range would be if you did not put an index that's
              within the string length...makes sense... so ignore my question to you
              in the prior post.
              >
              Thank you.
              >
              Family Tree Mike wrote:
              >By the way, the reason I said this is that I don't get an invalid cast,
              >but
              >rather
              >an index out of range error...
              >>

              Comment

              • raylopez99

                #8
                Re: Easy way* to cast an object in a LINQ query? Methinks not*

                Yes, thanks, that worked "OfType".

                I'm getting the hang of this Linq. Con permission, I would like to
                upload your Appendix A and also Albahari's Linq examples from Chapter
                8 of his excellent book "C#3.0 in a Nutshell". A little cheat sheet
                of Linq, since otherwise you have to buy a huge 1000 page tome (which
                I've ordered as well).

                If you're worried about copyright infringement I can work the examples
                some to make them 'unique', per our conversation about artistic
                license. Besides it's well known that supplying examples on the web
                will increase print sales...

                I like your book--so far up to p. 62 "Part 2".

                The best part so far IMO:

                - Listing 2.1 Using Delegates in a variety of simple ways. The best
                explanation of delegates I've seen so far, but, I will say by now I am
                pretty familiar with delegates / events so maybe that's why (with
                hindsight it seems so easy). But I'd never seen .Invoke in delegate
                before your book (normally it's not mentioned, since implicit).

                - p. 54 "Summary of Value types and Reference types" - this was good,
                but this *key* sentence should be amplified IMO "When a reference type
                is used as a method parameter, by default the parameter is passed _by
                value_--but the value itself is a reference". You should highlight
                and expound on "but the value itself is a reference" IMO because this
                is where newbies like me get confused. You should add--"..and in C#,
                you can change a reference type variable calling a method through a
                reference passed by value to a method, in exactly the same way as
                through a reference type passed by reference to the method, so long as
                'new' is not used in the method". Well, that's the idea, perhaps you
                can break up this compound sentence. What threw me (from a C++
                background) is the fact that they use copy constructors and const in C+
                + and I thought that 'pass by value' (for a reference type variable)
                meant a 'copy' that doesn't change the original--I'm sure other
                newbies have made this mistaken assumption too.

                Other stuff I like but I'll get to it later maybe.

                And Func (p.56) too I now like--now I get it--Albahari on p. 119 also
                discusses this useful syntax.

                Jon Skeet [ C# MVP ] wrote:
                See the "OfType" method.
                >
                >

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Easy way* to cast an object in a LINQ query? Methinks not*

                  On Sep 23, 12:56 am, raylopez99 <raylope...@yah oo.comwrote:
                  Yes, thanks, that worked "OfType".
                  Why are you calling AsQueryable() by the way?
                  I'm getting the hang of this Linq.  Con permission, I would like to
                  upload your Appendix A and also Albahari's Linq examples from Chapter
                  8 of his excellent book "C#3.0 in a Nutshell".  A little cheat sheet
                  of Linq, since otherwise you have to buy a huge 1000 page tome (which
                  I've ordered as well).
                  No, I'd rather you didn't do that. You'd have to ask Joe/Ben about C#
                  in a Nutshell, although it's worth checking whether the examples are
                  already available in downloadable source code.
                  If you're worried about copyright infringement I can work the examples
                  some to make them 'unique', per our conversation about artistic
                  license.  Besides it's well known that supplying examples on the web
                  will increase print sales...
                  That's a matter for myself and Manning to work out, however. Yes, I
                  like appendix A as well and think it's useful - but I would rather you
                  didn't take it upon yourself to publish it. If Manning and I choose to
                  make that available for free (there are already a couple of chapters
                  downloadable for free) then that's our decision to take. I will
                  mention it to the publisher though.

                  In terms of cheat sheets, you might want to look at http://refcardz.dzone.com
                  I like your book--so far up to p. 62 "Part 2".
                  >
                  The best part so far IMO:
                  >
                  - Listing 2.1 Using Delegates in a variety of simple ways.  The best
                  explanation of delegates I've seen so far, but, I will say by now I am
                  pretty familiar with delegates / events so maybe that's why (with
                  hindsight it seems so easy).  But I'd never seen .Invoke in delegate
                  before your book (normally it's not mentioned, since implicit).
                  I'm glad you're getting more comfortable with them - although I wish
                  you'd step away from the idea that they're like GOTO statements
                  (they're really not).
                  - p. 54 "Summary of Value types and Reference types" - this was good,
                  but this *key* sentence should be amplified IMO "When a reference type
                  is used as a method parameter, by default the parameter is passed _by
                  value_--but the value itself is a reference".  You should highlight
                  and expound on "but the value itself is a reference" IMO because this
                  is where newbies like me get confused.  You should add--"..and in C#,
                  you can change a reference type variable calling a method through a
                  reference passed by value to a method, in exactly the same way as
                  through a reference type passed by reference to the method, so long as
                  'new' is not used in the method".  Well, that's the idea, perhaps you
                  can break up this compound sentence.  What threw me (from a C++
                  background) is the fact that they use copy constructors and const in C+
                  + and I thought that 'pass by value' (for a reference type variable)
                  meant a 'copy' that doesn't change the original--I'm sure other
                  newbies have made this mistaken assumption too.
                  I originally had quite a long example with a blow-by-blow explanation
                  - I can't remember how much of it is still there. However, it's very
                  difficult to make this topic clear - I found your explanation above
                  very confusing (particularly uses of the word "through"). I have yet
                  to discover the "perfect" way of explaining the whole business (which
                  goes much deeper than just method parameters - it's fundamental to how
                  you understand variables, garbage collection, assignment, etc). I
                  haven't seen anyone else do a stellar job of explaining it either,
                  otherwise I'd defer to them. It's one of those things that just takes
                  a while before the lightbulb comes on.
                  Other stuff I like but I'll get to it later maybe.
                  >
                  And Func (p.56) too I now like--now I get it--Albahari on p. 119 also
                  discusses this useful syntax.
                  Not sure exactly what you're referring too - Func is a delegate type,
                  not a piece of syntax. I guess you mean either lambda expressions or
                  anonymous methods, but I don't have the book with me right now, so I
                  can't really tell.

                  Jon

                  Comment

                  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                    #10
                    Re: Easy way* to cast an object in a LINQ query? Methinks not*

                    raylopez99 wrote:
                    I am going through some LINQ queries.
                    No, you are not. You are using extension methods and lambda expressions,
                    but no LINQ.

                    --
                    Göran Andersson
                    _____
                    Göran Anderssons privata hemsida.

                    Comment

                    • raylopez99

                      #11
                      Re: Easy way* to cast an object in a LINQ query? Methinks not*

                      On Sep 22, 11:05 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                      On Sep 23, 12:56 am, raylopez99 <raylope...@yah oo.comwrote:
                      >
                      Yes, thanks, that worked "OfType".
                      >
                      Why are you calling AsQueryable() by the way?
                      I don't know--it was in the code I copied. I took it out and the
                      query works fine. Also I took out the cast and it works fine, but
                      raises a question**.

                      Here is the reduced query that works:

                      IEnumerable<str ingquery = words.OfType<st ring>().Select( str =>
                      str.Substring(0 , Math.Min(str.Le ngth, 2)));

                      **BTW, I found something of interest today. You can break a query
                      into two (see *below for an example), but, a question is raised:

                      if you have two queries separate, then the first one can trigger an
                      exception before the second one can override. What I mean is this:

                      it seems that in this query:
                      IEnumerable<str ingquery =
                      words.OfType<st ring>().Cast<st ring>().Select( str =str.Substring( 0,
                      2));

                      That the OfType<string>( ) 'overrides' the Cast<(because it seems
                      that with this functional type expression the final expression is not
                      evaluated until everything on the RHS is run). Thus you never get the
                      Exception thrown by .Cast<>, because .OfType<overrid es it.

                      But, if you were to break up this into two queries, what would
                      happen? Time to find out...wait...

                      OK, just as I thought: the exception is thrown! The *disadvantage*
                      of using multiple queries rather than all in one line.

                      Here it is:

                      // List<objectword s = new List<object{ "green",
                      "blue", 3, "violet", 5 };

                      IEnumerable<str ingquery1 =
                      words.Cast<stri ng>().Select(st r =str.Substring( 0,
                      Math.Min(str.Le ngth, 2))); //breaking up original query into 'query1'
                      and 'query2'

                      IEnumerable<str ingquery2 =
                      query1.OfType<s tring>(); // this is legal

                      foreach (string s in query2) //runtime error! since
                      query1 fails (throws an exception at element '3')

                      //ironically, breaking up the original query into two queries results
                      in a runtime error; had we left the original query alone we'd be
                      fine.


                      Another valuable contribution to theory.

                      No, I'd rather you didn't do that. You'd have to ask Joe/Ben about C#
                      in a Nutshell, although it's worth checking whether the examples are
                      already available in downloadable source code.

                      OK, no worries. Don't bother yourself asking, I just thought it would
                      be useful. You realize your publisher is like a wedding photographer
                      with your wedding pictures...if they're nice, it's not a problem, but
                      if they become hostile...
                      >
                      In terms of cheat sheets, you might want to look athttp://refcardz.dzone. com
                      Excellent. Thanks I signed up and downloaded them, very nice.
                      >
                      I like your book--so far up to p. 62 "Part 2".
                      >
                      The best part so far IMO:
                      >
                      - Listing 2.1 Using Delegates in a variety of simple ways.  The best
                      explanation of delegates I've seen so far, but, I will say by now I am
                      pretty familiar with delegates / events so maybe that's why (with
                      hindsight it seems so easy).  But I'd never seen .Invoke in delegate
                      before your book (normally it's not mentioned, since implicit).
                      >
                      I'm glad you're getting more comfortable with them - although I wish
                      you'd step away from the idea that they're like GOTO statements
                      (they're really not).
                      >
                      OK but they are "in my mind's eye".
                      - p. 54 "Summary of Value types and Reference types" - this was good,
                      but this *key* sentence should be amplified IMO "When a reference type
                      is used as a method parameter, by default the parameter is passed _by
                      value_--but the value itself is a reference".  You should highlight
                      and expound on "but the value itself is a reference" IMO because this
                      is where newbies like me get confused.  You should add--"..and in C#,
                      you can change a reference type variable calling a method through a
                      reference passed by value to a method, in exactly the same way as
                      through a reference type passed by reference to the method, so long as
                      'new' is not used in the method".  Well, that's the idea, perhaps you
                      can break up this compound sentence.  What threw me (from a C++
                      background) is the fact that they use copy constructors and const in C+
                      + and I thought that 'pass by value' (for a reference type variable)
                      meant a 'copy' that doesn't change the original--I'm sure other
                      newbies have made this mistaken assumption too.
                      >
                      I originally had quite a long example with a blow-by-blow explanation
                      - I can't remember how much of it is still there. However, it's very
                      difficult to make this topic clear - I found your explanation above
                      very confusing (particularly uses of the word "through"). I have yet
                      to discover the "perfect" way of explaining the whole business (which
                      goes much deeper than just method parameters - it's fundamental to how
                      you understand variables, garbage collection, assignment, etc). I
                      haven't seen anyone else do a stellar job of explaining it either,
                      otherwise I'd defer to them. It's one of those things that just takes
                      a while before the lightbulb comes on.
                      OK, that's interesting. I use the word "through" loosely, since I
                      never have figured out the convention in "calling method" or "called
                      method" or whatever. One code calls the other, I think the method,
                      like a procedure/sub procedure. Anyway, that's how I call it (mind's
                      eye).
                      >
                      Other stuff I like but I'll get to it later maybe.
                      >
                      And Func (p.56) too I now like--now I get it--Albahari on p. 119 also
                      discusses this useful syntax.
                      >
                      Not sure exactly what you're referring too - Func is a delegate type,
                      not a piece of syntax. I guess you mean either lambda expressions or
                      anonymous methods, but I don't have the book with me right now, so I
                      can't really tell.
                      >
                      Thanks. Also your book reviews are helpful. I think Albahari et al.
                      C# Nutshell reference book is the best C# book ever. Packed w/
                      information. Also useful is the C# Recipe book by O'Reilly.


                      Goodbye,

                      RL

                      ** // example showing a query broken up into two queries
                      for readability

                      string[] names = { "Tom", "Dick", "harry" };
                      IEnumerable<str ingfilteredName s1 =
                      System.Linq.Enu merable.Where(n ames, n =n.Length >= 4);

                      foreach (string n in filteredNames1)
                      Console.Write(n + "|"); //Dick|Harry|

                      IEnumerable<str ingfilteredName s2 =
                      System.Linq.Enu merable.Where(f ilteredNames1, n =n.Length <= 4);
                      Console.WriteLi ne("\n");
                      foreach (string n in filteredNames2)
                      Console.WriteLi ne(n + "|"); //Dick

                      Comment

                      • raylopez99

                        #12
                        Re: Easy way* to cast an object in a LINQ query? Methinks not*

                        On Sep 23, 6:54 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                        Haven't looked at the C# Recipe book. Lots more books to review... (I
                        don't think anyone can really say what "the best C# book ever" is
                        until they've read *all* of them, by the way.)
                        >
                        If you have the MSDN site link that loads when you load the Help in VS
                        (the front page) pls let me know--once your review was listed there.
                        For some reason I lost it and now I can't find it despite Google.

                        RL

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Easy way* to cast an object in a LINQ query? Methinks not*

                          On Sep 23, 4:09 pm, raylopez99 <raylope...@yah oo.comwrote:

                          <snip>
                           IEnumerable<str ingquery2 = OfType<string>( ).query;
                          >
                          This will not compile.  ANy other ideas?
                          Why would you expect that to compile? "query" isn't a member of
                          IEnumerable<Twh ich is returned by OfType<stringan d you need
                          something to call OfType<stringon in the first place.

                          I suggest you take a step back and try to understand extension methods
                          better so you can really pick apart what's going on in a LINQ query.
                          Experimenting by randomly reordering bits of code is going to be a
                          very time-consuming way of gaining any sort of understanding.

                          Jon

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Easy way* to cast an object in a LINQ query? Methinks not*

                            On Sep 23, 4:10 pm, raylopez99 <raylope...@yah oo.comwrote:
                            Haven't looked at the C# Recipe book. Lots more books to review... (I
                            don't think anyone can really say what "the best C# book ever" is
                            until they've read *all* of them, by the way.)
                            >
                            If you have the MSDN site link that loads when you load the Help in VS
                            (the front page) pls let me know--once your review was listed there.
                            For some reason I lost it and now I can't find it despite Google.
                            All my book reviews appear on my blog:


                            Some of them will appear on the VS C# channel, but not all - and I
                            won't always be able to predict which.

                            Jon

                            Comment

                            • Peter Morris

                              #15
                              Re: Easy way* to cast an object in a LINQ query? Methinks not*

                              >>
                              Pro LINQ: Language Integrated Query in C# 2008 (Windows.Net)
                              (Paperback) - 600 pages (rounds to 1000).
                              <<

                              hehe, if you round in ten thousands it rounds to zero. Why didn't you say
                              "That book with no pages" :-)

                              >>
                              "Thanks" for your "help". I didn't mean to attack you personally.
                              Yes I did.
                              <<

                              I haven't bothered to read this thread at all, I just happened to spot this
                              comment. Jon's a knowledgable and very helpful guy, why you would want to
                              attack him I don't know. If you're nice to people in here you'll get a lot
                              more help and learn a lot more. Or you can insult people and eventually
                              start to get ignored. For example, I stopped work for a few minutes to
                              think up the free/foreach example, write it, test it, just to make sure I
                              could paste code that you could execute. I wouldn't do that for someone
                              without manners.


                              Regards

                              Pete

                              Comment

                              Working...