?
Solved

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

Posted on 2010-11-22
7
Medium Priority
?
2,635 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 500 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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