C# sql xml列的linq查询
我需要为下面的sql查询用c#编写一个linq查询。 我在实现where、notin、orderby和descending方面没有问题,但问题是查询SQLXML列C# sql xml列的linq查询,c#,linq,C#,Linq,我需要为下面的sql查询用c#编写一个linq查询。 我在实现where、notin、orderby和descending方面没有问题,但问题是查询SQLXML列 SELECT [ID] ,[QueuedTime] ,Parameters.query('data(Root/Root/@type)').value('.', 'varchar(500)') as CommandName //how to implement this line in linq ,[Status]
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和order by descending条件。我关心的是如何实施
'Parameters.query('data(Root/Root/@type)')。值('.','varchar(500)'作为linq中的CommandName'
var unwantedStatus = new[] { 1, 2 };
var operationTimedOutTasks = context.TblRequestQueues
.Where(t => t.MethodName == "ProcessCommand" && !unwantedStatus.Contains(t.Status))
.OrderByDescending(t => t.ID)
.ToList();
尝试使用
XDocument
。(使用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();
以下内容解决了我的问题
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();
类似的方法可能会起作用,但我不确定它的性能如何。我遇到的错误是:“System.NotSupportedException:”LINQ to Entities无法识别“System.Xml.LINQ.XDocument Parse(System.String)”方法,并且该方法无法转换为存储表达式。“”