By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Null (?:)

P: n/a
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
Nov 20 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Thu, 20 Nov 2008 13:23:17 -0800, shapper <md*****@gmail.comwrote:
[...]
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
Nov 20 '08 #2

P: n/a
On Nov 20, 9:50*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 20 Nov 2008 13:23:17 -0800, shapper <mdmo...@gmail.comwrote:
[...]
* * * 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);
>>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.IsNullOrEmpty
(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 ...
Nov 20 '08 #3

P: n/a
shapper wrote:
On Nov 20, 9:50 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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
Nov 21 '08 #4

P: n/a
On Nov 21, 12:56*am, Arne Vajhj <a...@vajhoej.dkwrote:
shapper wrote:
On Nov 20, 9:50 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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 ...

Nov 21 '08 #5

P: n/a
On Thu, 20 Nov 2008 15:00:33 -0800, shapper <md*****@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.IsNullOrEmpty
(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("String", "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
Nov 21 '08 #6

P: n/a
On Thu, 20 Nov 2008 17:09:23 -0800, shapper <md*****@gmail.comwrote:
On Nov 21, 12:56*am, Arne Vajhøj <a...@vajhoej.dkwrote:
>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
Nov 21 '08 #7

P: n/a
On Nov 21, 1:16*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 20 Nov 2008 17:09:23 -0800, shapper <mdmo...@gmail.comwrote:
On Nov 21, 12:56*am, Arne Vajhj <a...@vajhoej.dkwrote:
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.IsNullOrEmpty
(l.Post.Excerpt) ? l.Post.Body : l.Post.Excerpt)).ToList();

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

I found it by chance ...
Nov 21 '08 #8

P: n/a
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.
Nov 21 '08 #9

P: n/a
On Thu, 20 Nov 2008 18:27:27 -0800, shapper <md*****@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
Nov 21 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.