Linq - 查询表并以 XML 形式返回结果

发布于 2024-09-12 11:27:12 字数 3793 浏览 3 评论 0原文

我有以下表结构:

CREATE TABLE [Report].[MesReport](
    [MesReportID] [int] IDENTITY(1,1) NOT NULL,
    [ParentID] [int] NOT NULL,
    [ReportTitle] [nvarchar](80) NOT NULL,
    [ReportName] [nvarchar](80) NOT NULL,
    [DatabaseServer] [nvarchar](80) NOT NULL,
    [DatabaseName] [nvarchar](50) NOT NULL,
    [Login] [nvarchar](80) NOT NULL,
    [ReportFile] [varbinary](max) NULL

我想创建一个 Linq 查询来查询该表并生成以下 XML。有人可以帮忙吗?

<MesReports> 
  <MesReport> 
      <ReportTitle>Mes to Sap Reconciliation Reports</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>1</MesReportId> 
      <ParentId>-1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Finished Goods</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>2</MesReportId> 
      <ParentId>1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Semi-Finished Goods</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>3</MesReportId> 
      <ParentId>1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Warranty Reports</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>4</MesReportId> 
      <ParentId>-1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Box - PBG</ReportTitle> 
      <ReportName>Warranty Box - PBG</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>5</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Box - FFO</ReportTitle> 
      <ReportName>Warranty Box - FFO</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>6</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Bin Count - PBG</ReportTitle> 
      <ReportName>Bin Count - PBG</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>7</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Bin Count - FFO</ReportTitle> 
      <ReportName>Bin Count - FFO</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>8</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
</MesReports> 

I have the following table structure:

CREATE TABLE [Report].[MesReport](
    [MesReportID] [int] IDENTITY(1,1) NOT NULL,
    [ParentID] [int] NOT NULL,
    [ReportTitle] [nvarchar](80) NOT NULL,
    [ReportName] [nvarchar](80) NOT NULL,
    [DatabaseServer] [nvarchar](80) NOT NULL,
    [DatabaseName] [nvarchar](50) NOT NULL,
    [Login] [nvarchar](80) NOT NULL,
    [ReportFile] [varbinary](max) NULL

I want to create a Linq query that will query this table and generate the following XML. Can someone help with this?

<MesReports> 
  <MesReport> 
      <ReportTitle>Mes to Sap Reconciliation Reports</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>1</MesReportId> 
      <ParentId>-1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Finished Goods</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>2</MesReportId> 
      <ParentId>1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Semi-Finished Goods</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>3</MesReportId> 
      <ParentId>1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Warranty Reports</ReportTitle> 
      <ReportName>None</ReportName>
      <DatabaseServer>None</DatabaseServer>
      <Database>None</Database>
      <Login>None</Login>
      <MesReportId>4</MesReportId> 
      <ParentId>-1</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Box - PBG</ReportTitle> 
      <ReportName>Warranty Box - PBG</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>5</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Box - FFO</ReportTitle> 
      <ReportName>Warranty Box - FFO</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>6</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Bin Count - PBG</ReportTitle> 
      <ReportName>Bin Count - PBG</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>7</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
  <MesReport> 
      <ReportTitle>Bin Count - FFO</ReportTitle> 
      <ReportName>Bin Count - FFO</ReportName>
      <DatabaseServer>MyServer</DatabaseServer>
      <Database>MESProduction</Database>
      <Login>6QDI+IoQbkZKJpVBcRJcNtoqR62606Q2</Login>
      <MesReportId>8</MesReportId> 
      <ParentId>4</ParentId> 
  </MesReport> 
</MesReports> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2024-09-19 11:27:12

这里的技巧是,您需要两个单独的 LINQ 语句,一个 linq-to-sql 来读取数据,一个 linq-to-xml 来写入数据。

var reportdata = from r in db.MesReport
//               where r.whatever ......
                select r;

var reportXmlItems = from rx in reportData
               select new XElement("MesReport", 
                    new XElement("ReportTitle", rx.ReportTitle),
                    new XElement("ReportName", rx.ReportName
                    new XElement("DatabaseServer", rx.DatabaseServer
                    new XElement("Database", rx.Database
                    new XElement("Login", rx.Login
                    new XElement("MesReportId", rx.MesReportId
                    new XElement("ParentId", rx.ParentId)
                );

 var reportXml = new XElement("MesReports", reportXmlItems);

The trick here is that you need two separate LINQ statement, one linq-to-sql to read the data, and one linq-to-xml to write it.

var reportdata = from r in db.MesReport
//               where r.whatever ......
                select r;

var reportXmlItems = from rx in reportData
               select new XElement("MesReport", 
                    new XElement("ReportTitle", rx.ReportTitle),
                    new XElement("ReportName", rx.ReportName
                    new XElement("DatabaseServer", rx.DatabaseServer
                    new XElement("Database", rx.Database
                    new XElement("Login", rx.Login
                    new XElement("MesReportId", rx.MesReportId
                    new XElement("ParentId", rx.ParentId)
                );

 var reportXml = new XElement("MesReports", reportXmlItems);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文