Null (?:)

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

    Null (?:)

    Hello,

    I have the following:

    postBook.Leafs = (from p in database.Posts
    select new PostLeaf {
    Post = p,
    }).ToPagedList( page.HasValue ?
    page.Value - 1 : 0, ResumePageSize) ;

    postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
    (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) );

    Basically, I need to define each post.Excerpt equal to post.Body if
    the post.Excerpt is empty.

    I don't have any compilation error but the excerpts that are null keep
    being null ...

    What am I doing wrong?

    Thanks,
    Miguel
  • Peter Duniho

    #2
    Re: Null (?:)

    On Thu, 20 Nov 2008 13:23:17 -0800, shapper <mdmoura@gmail. comwrote:
    [...]
    postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
    (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) );
    >
    Basically, I need to define each post.Excerpt equal to post.Body if
    the post.Excerpt is empty.
    >
    I don't have any compilation error but the excerpts that are null keep
    being null ...
    >
    What am I doing wrong?
    As is _so often_ the case, since you haven't posted a concise-but-complete
    code sample, there's no way to know.

    Based on the code you've shown, there's at least two possibilities, either
    of which might be case, both of which might be, or neither of which might
    be. That is, the Body property is also empty, or just as you've shown
    here you are in fact doing nothing at all with the projected result but
    rather are expecting the call to Select() to modify the enumerable on
    which it's called.

    Either of those would be a problem explaining your result. But, until you
    post a better question, there's no way to know.

    Pete

    Comment

    • shapper

      #3
      Re: Null (?:)

      On Nov 20, 9:50 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
      wrote:
      On Thu, 20 Nov 2008 13:23:17 -0800, shapper <mdmo...@gmail. comwrote:
      [...]
            postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
      (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) );
      >
      Basically, I need to define each post.Excerpt equal to post.Body if
      the post.Excerpt is empty.
      >
      I don't have any compilation error but the excerpts that are null keep
      being null ...
      >
      What am I doing wrong?
      >
      As is _so often_ the case, since you haven't posted a concise-but-complete  
      code sample, there's no way to know.
      >
      Based on the code you've shown, there's at least two possibilities, either  
      of which might be case, both of which might be, or neither of which might 
      be.  That is, the Body property is also empty, or just as you've shown  
      here you are in fact doing nothing at all with the projected result but  
      rather are expecting the call to Select() to modify the enumerable on  
      which it's called.
      >
      Either of those would be a problem explaining your result.  But, until you  
      post a better question, there's no way to know.
      >
      Pete
      No no Pete ... I posted almost all the code:

      postBook.Leafs = (from p in database.Posts
      let tags = p.PostsTags.Sel ect(pt =>
      pt.Tag).Default IfEmpty()
      orderby p.UpdatedAt descending
      where p.IsPublished == true
      select new PostLeaf {
      Post = p,
      Tags = tags.ToList(),
      TagsCSV = string.Join(", ", tags.Select(t =>
      t.Name).ToArray ())
      }).ToPagedList( page.HasValue ? page.Value -
      1 : 0, ResumePageSize) ;
      >>Here I have record 1 and 3 with body defined and excerpt
      empty and record 2 with both body and excerpt defined.

      postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
      (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) );
      >>Here I have exactly the same!
      This is why I don't understand what I am doing wrong. This is all the
      code I have. Yes, all 3 records are published.

      Then I preview on a web page and the 3 records shows exactly the
      result I get in debug ...

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Null (?:)

        shapper wrote:
        On Nov 20, 9:50 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
        wrote:
        >On Thu, 20 Nov 2008 13:23:17 -0800, shapper <mdmo...@gmail. comwrote:
        >>I don't have any compilation error but the excerpts that are null keep
        >>being null ...
        >>What am I doing wrong?
        >As is _so often_ the case, since you haven't posted a concise-but-complete
        >code sample, there's no way to know.
        No no Pete ... I posted almost all the code:
        complete = possible to save in file and compile as is

        Arne

        Comment

        • shapper

          #5
          Re: Null (?:)

          On Nov 21, 12:56 am, Arne Vajhøj <a...@vajhoej.d kwrote:
          shapper wrote:
          On Nov 20, 9:50 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
          wrote:
          On Thu, 20 Nov 2008 13:23:17 -0800, shapper <mdmo...@gmail. comwrote:
          >I don't have any compilation error but the excerpts that are null keep
          >being null ...
          >What am I doing wrong?
          As is _so often_ the case, since you haven't posted a concise-but-complete  
          code sample, there's no way to know.
          No no Pete ... I posted almost all the code:
          >
          complete = possible to save in file and compile as is
          >
          Arne
          Arne,

          The code I posted now is really all my code ...
          The only thing missing is the LinqToSql classes which is huge and
          nothing more than the automatic generated code by VS 2008 ...

          Comment

          • Peter Duniho

            #6
            Re: Null (?:)

            On Thu, 20 Nov 2008 15:00:33 -0800, shapper <mdmoura@gmail. comwrote:
            No no Pete ... I posted almost all the code:
            As Arne has pointed out, you definitely did not. You still haven't.

            But:
            [...]
            postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
            (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) );
            >
            >>Here I have exactly the same!
            Assuming that is the exact statement as found in your code, there is no
            more reason to expect that to do anything useful than this code would:

            string str = "My String";

            str.Replace("St ring", "Integer");

            Which was in fact one of the possibilities I described in my first reply.
            If that's not enough to help you, you _really_ need to post a
            _concise-but-complete_ code sample that demonstrates your problem.

            Pete

            Comment

            • Peter Duniho

              #7
              Re: Null (?:)

              On Thu, 20 Nov 2008 17:09:23 -0800, shapper <mdmoura@gmail. comwrote:
              On Nov 21, 12:56 am, Arne Vajhøj <a...@vajhoej.d kwrote:
              >shapper wrote:
              >[...]
              >complete = possible to save in file and compile as is
              >>
              >Arne
              >
              Arne,
              >
              The code I posted now is really all my code ...
              No, it's not. You cannot save it in a file and compile it to create a
              working program.
              The only thing missing is the LinqToSql classes which is huge and
              nothing more than the automatic generated code by VS 2008 ...
              The phrase "concise-but-complete" has TWO parts to it, both very
              important. "Complete" does not mean you post your entire program. It
              means that the code sample itself is "complete". The "concise" part is
              the clue that you need to put in the code sample _only_ those things that
              are relevant to demonstrating your program. In almost every case, this
              means that some of the code sample will provide placeholder data or will
              otherwise be simplified as compared to your actual code.

              Until you learn and live this mantra, you are going to continue to have
              trouble getting answers, either with respect to how easily and quickly
              they come, or with respect to whether you get answers at all.

              Pete

              Comment

              • shapper

                #8
                Re: Null (?:)

                On Nov 21, 1:16 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                wrote:
                On Thu, 20 Nov 2008 17:09:23 -0800, shapper <mdmo...@gmail. comwrote:
                On Nov 21, 12:56 am, Arne Vajhøj <a...@vajhoej.d kwrote:
                shapper wrote:
                [...]
                complete = possible to save in file and compile as is
                >
                Arne
                >
                Arne,
                >
                The code I posted now is really all my code ...
                >
                No, it's not.  You cannot save it in a file and compile it to create a  
                working program.
                >
                The only thing missing is the LinqToSql classes which is huge and
                nothing more than the automatic generated code by VS 2008 ...
                >
                The phrase "concise-but-complete" has TWO parts to it, both very  
                important.  "Complete" does not mean you post your entire program.  It  
                means that the code sample itself is "complete".  The "concise" part is 
                the clue that you need to put in the code sample _only_ those things that 
                are relevant to demonstrating your program.  In almost every case, this 
                means that some of the code sample will provide placeholder data or will  
                otherwise be simplified as compared to your actual code.
                >
                Until you learn and live this mantra, you are going to continue to have  
                trouble getting answers, either with respect to how easily and quickly  
                they come, or with respect to whether you get answers at all.
                >
                Pete
                Hi Pete,

                Sorry, I try to post the best I can ...

                But I just found the problem:

                postBook.Leafs. Select(l =l.Post.Excerpt = (String.IsNullO rEmpty
                (l.Post.Excerpt ) ? l.Post.Body : l.Post.Excerpt) ).ToList();

                I needed to add .ToList() at the end ...

                I found it by chance ...

                Comment

                • shapper

                  #9
                  Re: Null (?:)

                  Ironically, this is closely related to a question I asked you a very long 
                  time ago: why is it that you seem to want to use these LINQ methods for  
                  _everything_?  You are painting yourself into some corners that are silly  
                  at best, and are very wasteful at worst.
                  I am not trying to use Linq in everything anymore ... I followed your
                  advice.
                  To be honest, in this case, seemed logic to me ...

                  I just updated this with a loop.

                  It is not always easy, at least for me, to determine if I should use a
                  loop or a lambda expression.
                  Go for the code that works the best, and is the most maintainable.  
                  "Clever" doesn't get you any bonus points in programming, and it can make 
                  the code a lot worse if you're not careful.
                  I am careful ... that is why I ask you ...lol ... just joking.

                  Comment

                  • Peter Duniho

                    #10
                    Re: Null (?:)

                    On Thu, 20 Nov 2008 18:27:27 -0800, shapper <mdmoura@gmail. comwrote:
                    [...]
                    It is not always easy, at least for me, to determine if I should use a
                    loop or a lambda expression.
                    Okay, fair enough. I guess until you have more experience with the
                    language, that will be an issue. "Practice makes perfect". :)

                    Still, you would do well to try harder to post appropriate code examples.
                    You're not doing it now, and it's interfering with your ability to get the
                    best answer the first try.

                    Pete

                    Comment

                    Working...