Solved

How to I do a ToString inside LINQ

Posted on 2010-09-01
13
629 Views
Last Modified: 2013-12-17
The goal of this code is to write out XML.  The first section of code gathers the data.  The second attempts to write out the XML.  The code fails on converting a GUID to a string.

When the second section, the foreach, execute it throws this error:

“Unable to cast object of type 'System.Guid' to type 'System.String'”



var outboxMessages = (from x in outbox

                                  //var outboxString = from x in outbox

                                  join m in messageContext on x.MessageId equals m.MessageId

                                  join p in messageProperty on m.MessageId equals p.MessageId

                                  join a in messageArchive on p.MessageId equals a.MessageId

                                  where p.PropertyName == "DocumentNumber"

                                  select new {id = x.MessageId , m.MessageType, a.CreatedOn, x.QueuedOn, p.Value, 

                                      m.ReceivePortId, m.InboundTransportType}).ToList();



foreach (var i in outboxMessages)

            {

                outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id), new XAttribute("Type", i.MessageType),

                    new XAttribute("CreatedOn", i.CreatedOn), new XAttribute("ProcessedOn", i.QueuedOn),

                    new XAttribute("DocumentNumber", i.Value), new XAttribute("ReceivePortId", i.ReceivePortId),

                    new XAttribute("InboundTransportType", i.InboundTransportType)));

            }

Open in new window

0
Comment
Question by:Bruce
  • 6
  • 5
  • 2
13 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
Where's the GUID in your code?

Did you try "as string" ?
0
 
LVL 1

Author Comment

by:Bruce
Comment Utility
Sorry  

Here it is...
Attribute id

outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id),
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
did you try this

outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id) as string,
0
 
LVL 1

Author Comment

by:Bruce
Comment Utility
The code doesn’t even get that far.  The error is actually produced when the query executes.  However, it isn’t thrown until the foreach loop is initialized.
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 250 total points
Comment Utility
in the LINQ part change to

select new {id = x.MessageId.ToString(),...
0
 
LVL 1

Author Comment

by:Bruce
Comment Utility
I changed the linq to not even select the x.MessageId anymore, and it still throws that error.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
I'm not sure where it's going wrong. Paste the stack trace.
0
 
LVL 1

Author Comment

by:Bruce
Comment Utility
  at System.Data.SqlClient.SqlBuffer.get_String()
   at System.Data.SqlClient.SqlDataReader.GetString(Int32 i)
   at Read_<>f__AnonymousType5`6(ObjectMaterializer`1 )
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
   at MessageSink.MessageStatus.GetOutboxMessages(DataContext db) in N:\MessageSink\MessageStatus.cs:line 154
   at MessageSink.MessageStatus.LogMessageStatus() in N:\MessageSink\MessageStatus.cs:line 50
   at MessageSink.MainForm.SaveMessageStatus() in N:\MessageSink\MainForm.cs:line 199
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Can you post the query as you changed it and on what line of the code do you get the error on and the error message please.
0
 
LVL 1

Author Comment

by:Bruce
Comment Utility

The outboxMessages object contains the error as soon as the line of code is executed.

var outboxMessages = (from x in outbox

                                  //var outboxString = from x in outbox

                                  join m in messageContext on x.MessageId equals m.MessageId

                                  join p in messageProperty on m.MessageId equals p.MessageId

                                  join a in messageArchive on p.MessageId equals a.MessageId

                                  where p.PropertyName == "DocumentNumber"

                                  select new {id = x.MessageId , m.MessageType, a.CreatedOn, x.QueuedOn, p.Value, 

                                      m.ReceivePortId, m.InboundTransportType}).ToList();



The outboxMessages object contains the error as soon as the line of code is executed.

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
If we are talking about the error in the question, “Unable to cast object of type 'System.Guid' to type 'System.String'”", and if the only field in the select clause that is a Guid is x.MessageId then this should have corrected the issue :

id = x.MessageId.ToString() ,

but then you state, "I changed the Linq to not even select the x.MessageId anymore, and it still throws that error.", which tells me that there still is a field in the select clause that is a Guid. So can you please verify the data type of each of the fields in the select clause and post here.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
how many of your columns in the DB Table have Identity Columns and which one's you're using the in LINQ Expr.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

10 Experts available now in Live!

Get 1:1 Help Now