使用 UNION 的 LINQ-TO-XML

发布于 2024-08-20 07:43:13 字数 777 浏览 8 评论 0原文

我正在尝试联合查询来创建 XML 文件。查询本身作为 T-SQL 工作,但我作为 LINQ-TO-XML 的实现失败,并出现“无法翻译表达式...”错误。

我问错问题了吗?这种做法完全错误吗?我是 LINQ 新手。如何从 2 个查询创建单个 XML?

Dim db As New SOMEDataContext

Dim members As New XElement("members", _
    (From c In db.Employees _
    Join cf In db.BowlingTeams On c.ID Equals cf.BowlingTeam_Text _
        Where c.DEPARTMENT = "Housewares" _
            Select New XElement("member", _
                New XElement("id", c.ID), _
                New XElement("title", c.TITLE))) _
    .Union(From e In db.Employees _
        Where e.DEPARTMENT = "Housewares" _
        Where e.POSITION Like "*XYZ*" _
            Select New XElement("member", _
                New XElement("id", e.ID), _
                New XElement("title", e.TITLE))))

I am trying to union to queries to create an XML file. The query itself works as T-SQL, but my implementation as LINQ-TO-XML fails with "Could not translate expression..." error.

Am I asking the wrong question? Is this approach flat wrong? I am new to LINQ. How do I create a single XML from 2 queries?

Dim db As New SOMEDataContext

Dim members As New XElement("members", _
    (From c In db.Employees _
    Join cf In db.BowlingTeams On c.ID Equals cf.BowlingTeam_Text _
        Where c.DEPARTMENT = "Housewares" _
            Select New XElement("member", _
                New XElement("id", c.ID), _
                New XElement("title", c.TITLE))) _
    .Union(From e In db.Employees _
        Where e.DEPARTMENT = "Housewares" _
        Where e.POSITION Like "*XYZ*" _
            Select New XElement("member", _
                New XElement("id", e.ID), _
                New XElement("title", e.TITLE))))

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

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

发布评论

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

评论(2

兮颜 2024-08-27 07:43:13

下面是我在 VB 中执行此操作的方法:

    Dim members = <members>
                      <%= From c In db.Employees _
                          Join cf In db.BowlingTeams On c.ID Equals cf.BowlingTeam_Text _
                          Where c.DEPARTMENT = "Housewares" _
                          Select <member>
                                     <id><%= c.ID %></id>
                                     <title><%= c.TITLE %></title>
                                 </member> %>
                      <%= From e In db.Employees _
                          Where e.DEPARTMENT = "Housewares" _
                          Where e.POSITION Like "*XYZ*" _
                          Select <member>
                                     <id><%= e.ID %></id>
                                     <title><%= e.TITLE %></title>
                                 </member> %>
                  </members>

这利用了 XML Literals,从而避免了所有 New XElement 调用。

Here's how I would do that in VB:

    Dim members = <members>
                      <%= From c In db.Employees _
                          Join cf In db.BowlingTeams On c.ID Equals cf.BowlingTeam_Text _
                          Where c.DEPARTMENT = "Housewares" _
                          Select <member>
                                     <id><%= c.ID %></id>
                                     <title><%= c.TITLE %></title>
                                 </member> %>
                      <%= From e In db.Employees _
                          Where e.DEPARTMENT = "Housewares" _
                          Where e.POSITION Like "*XYZ*" _
                          Select <member>
                                     <id><%= e.ID %></id>
                                     <title><%= e.TITLE %></title>
                                 </member> %>
                  </members>

This takes advantage of XML Literals, which avoids the need for all the New XElement calls.

少跟Wǒ拽 2024-08-27 07:43:13

我认为你只需要做这样的事情。不确定 VB 语法是否正确。

   Dim query As From c In db.Employees Join cf In db.CourseFaculties On c.ID Equals cf.COURSEFACULTY_Text Where c.DEPARTMENT = "Housewares" Select New XElement("member", _
       New XElement("id", c.ID), New XElement("title", c.TITLE))).Union(From e In db.DATATEL_Employees Where e.DEPARTMENT = "Housewares" Where e.POSITION Like "*XYZ*" _
       Select New XElement("member", New XElement("id", e.ID), New XElement("title", e.TITLE)));

   Dim members As New XElement("members");
   foreach (XElement result in query)
       members.Add(result);

I think you just have to do something like this. Not sure if the VB syntax is correct.

   Dim query As From c In db.Employees Join cf In db.CourseFaculties On c.ID Equals cf.COURSEFACULTY_Text Where c.DEPARTMENT = "Housewares" Select New XElement("member", _
       New XElement("id", c.ID), New XElement("title", c.TITLE))).Union(From e In db.DATATEL_Employees Where e.DEPARTMENT = "Housewares" Where e.POSITION Like "*XYZ*" _
       Select New XElement("member", New XElement("id", e.ID), New XElement("title", e.TITLE)));

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