Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to I do a ToString inside LINQ

Posted on 2010-09-01
13
Medium Priority
?
645 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

972 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