开发者

Creating an XElement item in a Linq expression

开发者 https://www.devze.com 2023-01-06 06:13 出处:网络
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)” lin

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();


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消