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,566 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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