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();
1
  • Something like this might work, although I'm not sure how performant it would be Commented Feb 19, 2018 at 15:14

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.