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,525 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_
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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