使用 UNION 的 LINQ-TO-XML
我正在尝试联合查询来创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是我在 VB 中执行此操作的方法:
这利用了 XML Literals,从而避免了所有
New XElement
调用。Here's how I would do that in VB:
This takes advantage of XML Literals, which avoids the need for all the
New XElement
calls.我认为你只需要做这样的事情。不确定 VB 语法是否正确。
I think you just have to do something like this. Not sure if the VB syntax is correct.