Link to home
Start Free TrialLog in
Avatar of Bruce
BruceFlag for United States of America

asked on

How to I do a ToString inside LINQ

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

Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Where's the GUID in your code?

Did you try "as string" ?
Avatar of Bruce

ASKER

Sorry  

Here it is...
Attribute id

outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id),
did you try this

outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id) as string,
Avatar of Bruce

ASKER

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.
SOLUTION
Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce

ASKER

I changed the linq to not even select the x.MessageId anymore, and it still throws that error.
I'm not sure where it's going wrong. Paste the stack trace.
Avatar of Bruce

ASKER

  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
Avatar of Fernando Soto
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.
Avatar of Bruce

ASKER


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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how many of your columns in the DB Table have Identity Columns and which one's you're using the in LINQ Expr.