Solved

How to I do a ToString inside LINQ

Posted on 2010-09-01
13
633 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
ID: 33578920
Where's the GUID in your code?

Did you try "as string" ?
0
 
LVL 1

Author Comment

by:Bruce
ID: 33579540
Sorry  

Here it is...
Attribute id

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

Expert Comment

by:Gururaj Badam
ID: 33579558
did you try this

outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id) as string,
0
DevOps Toolchain Recommendations

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

 
LVL 1

Author Comment

by:Bruce
ID: 33579661
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
ID: 33579707
in the LINQ part change to

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

Author Comment

by:Bruce
ID: 33579740
I changed the linq to not even select the x.MessageId anymore, and it still throws that error.
0
 
LVL 8

Expert Comment

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

Author Comment

by:Bruce
ID: 33579825
  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
ID: 33580434
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33580474
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
ID: 33581197

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
ID: 33581660
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
ID: 33584231
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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