簡體   English   中英

使用 LINQ 和 XElement 查詢 SQL 表

[英]Querying SQL table with LINQ and XElement

我不知道這是否可行,但是,我想從 SQL 中的表中檢索一些字段以及表中 XML 字段中的特定值,例如

MyTable(UserName varchar(50),XML_Response XML)

注意:C# 中XML_Response字段的值的數據類型是XElement

領域內的XML XML_Response波紋管可以有兩種形式| 標簽內沒有值更新結果說明:

<IntegrationResults>
  <UpdateResult>Failure</UpdateResult>
  <UpdateResultDescription>LeadId 2876474 not found</UpdateResultDescription>
</IntegrationResults>
<IntegrationResults>
    <UpdateResult>Success</UpdateResult>
    <UpdateResultDescription />
</IntegrationResults>

我的查詢輸出我想看起來像這樣(以 2 行為例)

"My User Name","LeadId 83608 not found"
"My User Name 2",""

先感謝您

作為您嘗試的替代方法,您可以使用 C# 命令對象並直接在表上運行以下 SQL:

SELECT UserName
      , IntegrationResults.n.value('UpdateResultDescription[1]','VARCHAR(200)') as UpdateResultDescription
  FROM  MyTable
        outer apply MyTable.XML_Response.nodes('/IntegrationResults') AS IntegrationResults(n);

謝謝大家,我找到了解決辦法:

.select(x => new
{
   x.UserName,
   UpdateResult = x.MyTable.XML_Response.Element("UpdateResult").Value,
   UpdateResultDescription = x.MyTable.XML_Response.Element("UpdateResultDescription").Value,
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM