【问题标题】:linq query for sql xml columnsql xml列的linq查询
【发布时间】:2018-02-19 15:04:14
【问题描述】:

我需要在 c# 中为以下 sql 查询编写一个 linq 查询。 我在实现 where,not in,orderby 和 descending 方面没有问题,但问题是查询 sql xml 列

 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 

下面的查询将满足 where,not in 和降序条件。我担心如何实施 '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();

【问题讨论】:

  • this 之类的东西可能会起作用,尽管我不确定它的性能如何

标签: c# linq


【解决方案1】:

以下解决了我的问题。

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

【讨论】:

    【解决方案2】:

    试试XDocument。 (using System.Xml.Linq;)

    例子:

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

    【讨论】:

    • 我想出的错误是:'System.NotSupportedException: 'LINQ to Entities 无法识别方法'System.Xml.Linq.XDocument Parse(System.String)' 方法,而这个方法无法转换为商店表达式。''
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多