Bruce
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'”
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)));
}
ASKER
Sorry
Here it is...
Attribute id
outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id),
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,
outboxXML.Add(new XElement("Message", new XAttribute("Id", i.id) as string,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
at System.Data.SqlClient.SqlB uffer.get_ String()
at System.Data.SqlClient.SqlD ataReader. GetString( Int32 i)
at Read_<>f__AnonymousType5`6 (ObjectMat erializer` 1 )
at System.Data.Linq.SqlClient .ObjectRea derCompile r.ObjectRe ader`2.Mov eNext()
at MessageSink.MessageStatus. GetOutboxM essages(Da taContext db) in N:\MessageSink\MessageStat us.cs:line 154
at MessageSink.MessageStatus. LogMessage Status() in N:\MessageSink\MessageStat us.cs:line 50
at MessageSink.MainForm.SaveM essageStat us() in N:\MessageSink\MainForm.cs :line 199
at System.Data.SqlClient.SqlD
at Read_<>f__AnonymousType5`6
at System.Data.Linq.SqlClient
at MessageSink.MessageStatus.
at MessageSink.MessageStatus.
at MessageSink.MainForm.SaveM
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
how many of your columns in the DB Table have Identity Columns and which one's you're using the in LINQ Expr.
Did you try "as string" ?