473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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
9 1478
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
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
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
On Nov 21, 12:56*am, Arne Vajhøj <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
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
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
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 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
5
by: Mike MacSween | last post by:
This as the row source for a combo: SELECT qryRole.RoleID, qryRole.Role FROM qryRole WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.