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

J

Jon Skeet [C# MVP]

raylopez99 said:
I know it's obvious why it DOESN'T work, but the question was: (1) is
it easy to make it work with a one line change? if not, then your
reluctance to provide a short answer is understandable; but, if so,
then (2) it follows you must be incompetent or obstinant.

I'm obstinate that the best way for you to learn is not through us just
correcting your mistakes without you making any effort to learn the
basics of what the code means. Cargo cultism is really not a good way
to get a good grasp of the language.

Heck, you managed to get code which would compile before - you just
messed up the ordering.
Pro LINQ: Language Integrated Query in C# 2008 (Windows.Net)
(Paperback) - 600 pages (rounds to 1000).

Maybe I should try asking my employer to do that kind of rounding.
Rounding 580 pages up to 600 isn't unreasonable, but rounding 600 up to
1000 is.
I understand extension methods. It's code that has a .dot in front of
it, that you can customize, like .ToPrint(). It's not considered good
form, unless absolutely necessary.

You clearly *don't* understand them properly if you're trying to write
code like  "IEnumerable said:
"Thanks" for your "help". I didn't mean to attack you personally.
Yes I did.

Um?
 
R

raylopez99

I'm obstinate that the best way for you to learn is not through us just
correcting your mistakes without you making any effort to learn the
basics of what the code means. Cargo cultism is really not a good way
to get a good grasp of the language.

Cargo cultism? You are making an obscure cultural reference of the
same kind that you faulted that author of that 1000 page LINQ tome for
in your review. As I recall Papua New Guinea tribesmen supposedly
waved their hands like cargo plane handlers hoping goods would drop
out of the sky, confusing cause and effect, but how many people would
know this?

I do know the basics--possibly better than you. I've coded in Fortran
(77!), Basic, Visual Basic, C, C++ (unmanaged and CLI), C# and Java
1.0 (OK it was a Hello World). When you were in diapers, youngster.
Maybe I should try asking my employer to do that kind of rounding.
Rounding 580 pages up to 600 isn't unreasonable, but rounding 600 up to
1000 is.

YOu making six figures son? You should be. Competent programmers
make this much--and they don't have time to post here (oops! sorry,
didn't mean it that way; hey I post here for fun and to learn). I got
an offer for 225k USD--but I don't know if I should take it, I'm
having so much fun running my own business... Money is good here.
Plus it's in Asia, which I like but it's kind of stressful.
You clearly *don't* understand them properly if you're trying to write
code like  "IEnumerable<string> query2 = OfType<string>().query;"

Ok I will confess: I did that just to show Marc the French Coffee
drinker his suggestion did not trivially work--hoping he --or you--
would come back with a one liner that solved everything. But instead
we're slogging it out in this flame war. My mistake. Live and lern.

This is an unmoderated newsgroup; flaming is OK on occasion. It blows
off steam. In the early days of the Internet you could even make
death threats and get away with it, before they changed the law. Ah,
those were the days...

RL
 
P

Peter Morris

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
 
J

Jon Skeet [C# MVP]

<snip>

As I've said before, I won't offer any help while you're being rude. If
you're still interested in the topic, put a bit of effort (five minutes
ought to do it) and try again - then post a *civil* message if you have
any more problems.
 
R

raylopez99

OK "sorry" for not being (in your mind's eye) "civil". My apologies.

Now, yes, I found a solution. Here it is:

List<object> words = new List<object> { "green", "blue", 3, "violet",
5 };

IEnumerable<string> query = words.OfType<string>().Select(str =>
str.Substring(0, Math.Min(str.Length, 2)));
IEnumerable<string> query2 = query.Cast<string>();
//works because cast follows OfType (always safer since Cast throws
exception while OfType does not)

You simply have to remember that OfType should preceed Cast and not
follow it, because OfType does not throw an exception, while Cast
does.

I even just noticed that in your Appendix A of your book you mention
this (Table A.3).

But a larger question is this--given a try/catch block, how can you
catch an exception that's of type InvalidCastException, and then
continue in the 'try' block after the exception is thrown? Pseudocode
or even just a short answer in words is OK, just a hint is all I'm
seeking.

"Thanks", "in advance".

RL
 
J

Jon Skeet [C# MVP]

OK "sorry" for not being (in your mind's eye) "civil".  My apologies.

Thank you. And clearly not just in my mind's eye, either - see the
response from Peter Morris.
Now, yes, I found a solution.  Here it is:

List<object> words = new List<object> { "green", "blue", 3, "violet",
5 };

IEnumerable<string> query = words.OfType<string>().Select(str =>
str.Substring(0, Math.Min(str.Length, 2)));
IEnumerable<string> query2 = query.Cast<string>();
 //works because cast follows OfType (always safer since Cast throws
exception while OfType does not)

You simply have to remember that OfType should preceed Cast and not
follow it, because OfType does not throw an exception, while Cast
does.

You should also remember that using Cast after OfType is basically
pointless (assuming you're supplying the same type argument in both
places). You're saying, "Give me all the strings from the sequence"
and then "make sure everything is a string". What benefit do you
believe the call to Cast is providing?
I even just noticed that in your Appendix A of your book you mention
this (Table A.3).

But a larger question is this--given a try/catch block, how can you
catch an exception that's of type InvalidCastException, and then
continue in the 'try' block after the exception is thrown?  Pseudocode
or even just a short answer in words is OK, just a hint is all I'm
seeking.

You'd have to use a loop - and because the exception happens in the
implicit call to MoveNext() supplied by foreach, you'd pretty much
have to write the iteration by hand. However, that would only work if
the iterator then moved on beyond the "broken" element. I don't know
offhand whether it does. Continuing to use an iterator after it's
thrown an exception strikes me as a very bad idea.

Jon
 
M

Marc Gravell

However, that would only work if
the iterator then moved on beyond the "broken" element. I don't know
offhand whether it does.

For an iterator block, it won't. The generated code sets the state to
EOF at the start of every MoveNext(), and only puts it back to
something meaningful if the block completes - saves on a try/catch I
guess.

So it certainly isn't a safe approach, even if it happens to work for
some rare systems.

Marc
 
R

Registered User

But a larger question is this--given a try/catch block, how can you
catch an exception that's of type InvalidCastException, and then
continue in the 'try' block after the exception is thrown? Pseudocode
or even just a short answer in words is OK, just a hint is all I'm
seeking.

The simple answer is to re-organize the code so that the desired point
of continuation follows the catch block. The complex answer questions
the overall design that requires the initial question being asked.

regards
A.G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top