tags:

views:

20

answers:

1

I’m using Linq to create a list of objects. Shown below is the expression I am using. The problem I have is that at runtime it isn’t happy with the “RawXMLDocument = XElement.Parse(t.Message)” line. The “RawXMLDocument” property takes an XElement. Is there a way to convert the string version of the XML (held in Message) into an XElement as shown below?

var transactionList = (from t in ctx.Transactions
                   where t.MessageRecievedAt >= Start && t.MessageRecievedAt <= End
                   select new TerminalTransactionMessage
                   {
                       DeviceId = t.DeviceId,
                       RawXMLDocument = XElement.Parse(t.Message),
                       TransactionTime = t.EventTime_Local
                   }).ToList();
+2  A: 

Is the problem that it's trying to do it in SQL? If so, you need to just separate out the bits to do in SQL and the bits to do in LINQ to Objects. Try this:

var query = from t in ctx.Transactions
            where t.MessageRecievedAt >= Start && t.MessageRecievedAt <= End
            select new
            {
                t.DeviceId,
                t.Message,
                TransactionTime = t.EventTime_Local
            };
var transactionList = query.AsEnumerable() // LINQ to Objects from here
                           .Select(t => new TerminalTransactionMessage 
                                   {
                                      DeviceId = t.DeviceId,
                                      RawXMLDocument = XElement.Parse(t.Message),
                                      TransactionTime = t.TransactionTime
                                   });
                           .ToList();
Jon Skeet
That did the trick, many thanks.
Retrocoder