1

I would need to write a linq query in c# for the following sql query. I have no issues in implementing where, not in, orderby and descending but the problem is to query a sql xml column

 SELECT [ID]
   ,[QueuedTime]
   ,Parameters.query('data(Root/Root/@type)').value('.', 'varchar(500)') as CommandName //how to implement this line in linq
   ,[Status]
   ,[CurrentRetryCount]
   ,[MaxRetryCount]
   ,[RetryDelaySeconds]
   ,[CompletedTime]
   ,[LastTriedTime]
   ,[LastError]
   ,Parameters
   ,[PrincipalString]
   FROM [AppServer].[dbo].[RequestQueue]
   where interfacename = 'ICommunicationsService'
  and MethodName = 'ProcessCommand'
  and status not in (1,2)
   order by id desc 

The following query will meet where, not in and order by descending conditions. I am concerned about how do I implement 'Parameters.query('data(Root/Root/@type)').value('.', 'varchar(500)') as CommandName' in linq

   var unwantedStatus = new[] { 1, 2 };

   var operationTimedOutTasks = context.TblRequestQueues
                                                .Where(t => t.MethodName == "ProcessCommand" && !unwantedStatus.Contains(t.Status))
                                                .OrderByDescending(t => t.ID)                                                
                                                .ToList();
ChinnaR
  • 797
  • 3
  • 9
  • 24
  • Something like [this](https://stackoverflow.com/a/1832756/5443550) might work, although I'm not sure how performant it would be – Diado Feb 19 '18 at 15:14

2 Answers2

2

The following has resolved my issue.

var query = from queue in context.TblRequestQueues
                                               where queue.MethodName == methodName
                                               && queue.InterfaceName == interfaceName
                                               && !unwantedStatus.Contains(queue.Status)
                                               orderby queue.ID descending
                                               select new
                                               {
                                                   queue.QueuedTime,
                                                   queue.Parameters,
                                                   queue.Status,
                                                   queue.CurrentRetryCount,
                                                   queue.MaxRetryCount,
                                                   queue.RetryDelaySeconds,
                                                   queue.CompletedTime,
                                                   queue.LastTriedTime,
                                                   queue.LastError,
                                                   queue.PrincipalString
                                               };
                var operationTimedOutTasks = query.AsEnumerable()
                                                .Select(t => new TblRequestQueueDto
                                                {
                                                    QueuedTime = t.QueuedTime,
                                                    Parameters = t.Parameters,
                                                    CommandName = XDocument.Parse(t.Parameters).Element("Root").Descendants("Root").FirstOrDefault().Attribute("type").Value,
                                                    Status = t.Status,
                                                    CurrentRetryCount = t.CurrentRetryCount,
                                                    MaxRetryCount = t.MaxRetryCount,
                                                    RetryDelaySeconds = t.RetryDelaySeconds,
                                                    CompletedTime = t.CompletedTime,
                                                    LastTriedTime = t.LastTriedTime,
                                                    LastError = t.LastError,
                                                    PrincipalString = t.PrincipalString
                                                }).ToList();
ChinnaR
  • 797
  • 3
  • 9
  • 24
0

Try with XDocument. (using System.Xml.Linq;)

Example:

var operationTimedOutTasks = (from queue in context.TblRequestQueues
                              where queue.MethodName == "ProcessCommand" 
                                  && !unwantedStatus.Contains(t.Status)
                              let xml = XDocument.Parse(queue.Parameters)
                              orderby queue.ID
                              select new 
                              {
                                //Other columns
                                Parameters = xml.Descendants("Root").FirstOrdDefault().Attribute("type").Value
                              }).ToList();
Sunil
  • 3,404
  • 10
  • 23
  • 31
  • 2
    The error I am coming up with is: 'System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.Xml.Linq.XDocument Parse(System.String)' method, and this method cannot be translated into a store expression.'' – ChinnaR Feb 21 '18 at 09:02