2010-02-03 126 views
1

我想聯合查詢來創建一個XML文件。查詢本身就像T-SQL一樣工作,但是我的作爲LINQ-TO-XML的實現失敗,出現「Could not translate expression ...」錯誤。使用UNION的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)))) 

回答

1

這是我會怎麼做,在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文本,它避免了所有的New XElement調用需要的優勢。

0

我想你只需要做這樣的事情。不確定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);