2016-05-21 8 views
0

Angesichts der unten genannten XML muss ich alle Mitarbeiter, die zu einer Abteilung gehören zurückgeben. Also, wenn Department = Mode sollte 3 Mitarbeiter RückkehrMitarbeiter zurückgeben mit Linq zu xml

<?xml version="1.0" encoding="utf-8" ?> 
    <Store> 
     <Departments> 
     <Department name="Fashion"> 
      <Employees> 
      <Employee FirstName="Jo" Surname="Blogg"/> 
      <Employee FirstName="Mark" Surname="Smith"/> 
      <Employee FirstName="Rose" Surname="Blogg2"/> 
      </Employees> 
     </Department>  
     <Department name="Makeup"> 
      <Employees>  
      <Employee FirstName="Sonia" Surname="Smith2"/> 
      <Employee FirstName="Jenny" Surname="Blogg3"/> 
      </Employees> 
     </Department> 
    </Departments> 
    </Store> 

, was ich versucht habe, aber nicht kompilieren und andere Versuche waren nicht am gewünschten Ergebnis

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Xml.Linq; 

    namespace ConsoleApplicationXml 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       var xDocument = XDocument.Load("Store.xml"); 

       //Get all employees that belong to "Fashion 
       string departmentName = "Fashion"; 

     //compiles but get object variable not set 
     var employees = (from emp in xDocument.Descendants("Employees") 
         where (emp.Parent.FirstAttribute.Value == departmentName) 

         select new Employee 
         { 
          DepartmentName = departmentName, 
          FirstName = emp.FirstAttribute.Value, 
          Surname = emp.LastAttribute.Value 
         }).ToList(); 

// NICHT KOMPILIEREN zurückkehren !!

   var employees = (from emp in xDocument.Descendants("Employees") 
           where (emp.Parent.FirstAttribute.Value == departmentName) 
           let xFirstName = emp.Element("Employee").FirstAttribute("FirstName") 
           let xLastName = emp.Element("LastName") 
        select new Employee 
        { 
         DepartmentName = departmentName, 
         FirstName = xFirstName.Value, 
         Surname = xLastName.Value 
        }).ToList(); 
      } 
     } 

     public class Employee 
     { 
      public string DepartmentName { get; set; } 
      public string FirstName { get; set; } 
      public string Surname { get; set; } 
     } 
    } 

Antwort

0

Sie können mit zwei from Klausel aka SelectMany()Department Elemente filtern und wählen Sie Employee Elemente entsprechen:

var employees = (from department in xDocument.Descendants("Department") 
       from emp in department.Descendants("Employee") 
       where department.Attribute("name").Value == departmentName 
       select new Employee 
       { 
        DepartmentName = departmentName, 
        FirstName = emp.Attribute("FirstName").Value, 
        Surname = emp.Attribute("Surname").Value 
       }).ToList(); 
+0

Hallo danke, das funktioniert definitiv. Willst du damit sagen, dass du auch viele direkt auswählen und die 2 from-Klauseln eliminieren kannst? – developer9969

+0

Ja, 'SelectMany()' ist die äquivalente Methode für die 2 'from' -Klauseln im Abfragestil: http://stackoverflow.com/questions/6414816/is-there-ac-sharp-linq-syntax-for-the -queryable-selectmany-method – har07

0

In Ihrem ersten Beispiel

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Linq; 

namespace ConsoleApplicationXml 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var xDocument = XDocument.Load("Store.xml"); 

      //Get all employees that belong to "Fashion 
      string departmentName = "Fashion"; 

    //compiles but get object variable not set 
    var employees = (from emp in xDocument.Descendants("Employees") 
        where (emp.Parent.FirstAttribute.Value == departmentName) 

        select new Employee 
        { 
         DepartmentName = departmentName, 
         FirstName = emp.FirstAttribute.Value, 
         Surname = emp.LastAttribute.Value 
        }).ToList(); 

, was Sie falsch zu machen, ist, dass die emp Variable ist für die Mitarbeiter Tag, daher haben FirstAttribute und LastAttribute, die Sie verwenden möchten, keine Bedeutung. Verwenden Sie stattdessen diesen Code:

var employeesParent = xDocument.Descendants("Employees").Where(element => element.Parent.Attribute("name").Value == departmentName); 
var employees = (from emp in employeesParent.Descendants() 
         select new Employee 
         { 
          DepartmentName = departmentName, 
          FirstName = emp.Attribute("FirstName").Value, 
          Surname = emp.Attribute("Surname").Value 
         }).ToList(); 

Ich hoffe, es hilft.

+0

danke, dass funktioniert auch.Sie antworten hilft mir zu verstehen, wo ich falsch ging.Vielen Dank. Ich habe bereits die andere Antwort akzeptiert, die vor deiner kam. – developer9969