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,460 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now