Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to I do a ToString inside LINQ

Posted on 2010-09-01
13
Medium Priority
?
644 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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 1000 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 1000 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 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