开发者

LINQ-TO-XML using UNION

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))))


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜