Null (?:)

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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.IsNullOrEmpty
(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
 
[...]
      postBook.Leafs.Select(l => l.Post.Excerpt = (String.IsNullOrEmpty
(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.Select(pt =>
pt.Tag).DefaultIfEmpty()
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);
empty and record 2 with both body and excerpt defined.

postBook.Leafs.Select(l => l.Post.Excerpt = (String.IsNullOrEmpty
(l.Post.Excerpt) ? l.Post.Body : l.Post.Excerpt));

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 ...
 
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 ...
 
shapper wrote:
[...]
complete = possible to save in file and compile as is
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.IsNullOrEmpty
(l.Post.Excerpt) ? l.Post.Body : l.Post.Excerpt)).ToList();

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

I found it by chance ...
 
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.
 
Back
Top