Solved

"Unable to case object of type 'System.Decimal' to type 'System.String' occuring within a LINQ statement.

Posted on 2010-11-22
7
2,538 Views
Last Modified: 2013-11-11
The following LINQ statement is generating the error.

                        IEnumerable<String> keyValue = ((IEnumerable<String>)
                            from DataRow rows in dtPrimary.Rows
                            where rows[currentGroup.Peek().ColumnName.ToString()].ToString() == node.ToString()
                            select (String)rows[key]).Take(1).ToArray();

The exception is occuring in the select portion: select (String) rows[key].Take(1).ToArray() due to the fact that several of the columns in dtPrimary are of type System.Decimal. When I initially wrote the code I was only interested in the Strings, however.  Is there away for me to handle this without doing IEnumerable<object>?  It would be best for me just to have all the decimal (or ints) convert to a string so that client code doesn't have to change.


Regards,


_Wade_
0
Comment
Question by:_Wade_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34190558
Try select rows[key].ToString().Take(1).ToArray();

I think that should either give you the string representation of a decimal, or if the value was already a string will just get you that string.
0
 

Assisted Solution

by:_Wade_
_Wade_ earned 0 total points
ID: 34190578
Doh!

IEnumerable<String> keyValue = ((IEnumerable<String>)
                            from DataRow rows in dtPrimary.Rows
                            where rows[currentGroup.Peek().ColumnName.ToString()].ToString() == node.ToString()
                            select Convert.ToString(rows[key])).Take(1).ToArray();

Please! Please! no one laugh...   I was up late last night.

:p

Thanks to anyone who took the time to read.
0
 
LVL 11

Accepted Solution

by:
jasonduan earned 125 total points
ID: 34190605
I would do something like:
IEnumerable<String> keyValue =  (from DataRow row in dt.AsEnumerable()
                    where ...
                    .select row[key].ToString()
                    ).ToList();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:_Wade_
ID: 34190617
No, that gives the exception:

Unable to cast object of type 'WhereSelectEnumarableIterator`2[System.Data.DataRow.System.Object]` to type 'System.Collections.Generic.IEnumerable1[System.String]'.


But thanks for the response tqerbert!
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34190674
Since everything in .Net implements ToString() method, it's always safe to call - I think I just made a typo and missed a parentheses. ;)

Either way, you came to about the same conclusion on your own anyway (I would guess that the implementation of Convert.ToString(Decimal d) mostly just does return d.ToString())
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34190736
Incidentally, I see you're using .Take(1) to get just the first result - if your intention is to retrieve only the value of the first item in the result set, it might make more sense to write:

string result = (from DataRow rows in dtPrimary.Rows
                       where rows[currentGroup.Peek().ColumnName.ToString()].ToString() == node.ToString()
                       select rows[key].ToString()).FirstOrDefault();

Open in new window

0
 

Author Closing Comment

by:_Wade_
ID: 34221267
Jason, I like your way a little better than mine so I'm marking yours best, but I still get to keep half the points for answering my own question. :)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question