Solved

How to I do a ToString inside LINQ

Posted on 2010-09-01
13
642 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 63

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 63

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

630 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