2016-01-19 18 views
5

मैं एक XML फ़ाइल को deserializing और मॉडल ऑब्जेक्ट कर रहा हूँ। यद्यपि यह वास्तविक मॉडल नहीं है, नीचे संरचना के समान है जो मेरे पास है।नेस्टेड ऑब्जेक्ट से मूल्यों की विशिष्ट सूची प्राप्त करें

[Serializable()] 
[System.Xml.Serialization.XmlRoot("AutoEnvelope")] 
public class AutoBody 
{ 
    [XmlArray("AutoBody")] 
    [XmlArrayItem("Vehicles", typeof(Vehicles))] 
    public Vehicles[] Vehicles { get; set; } 

} 

[Serializable()] 
public class Vehicles 
{ 
    [XmlElement("SelectedCar", typeof(SelectedCar))] 
    public SelectedCar SelectedCar { get; set; } 

    [XmlElement("OfferedVehicles", typeof(OfferedVehicles))] 
    public OfferedVehicles OfferedVehicles { get; set; } 

} 

[Serializable()] 
public class SelectedCar 
{ 
    [System.Xml.Serialization.XmlElement("Model")] 
    public string Model { get; set; } 

    [System.Xml.Serialization.XmlElement("NumTires")] 
    public int NumTires { get; set; } 

    [System.Xml.Serialization.XmlElement("Color")] 
    public string Color { get; set; } 

} 

मैं चयनित कर की अलग सूची प्राप्त करने की कोशिश कर रहा हूं। रंग मूल्य और असफल रहा है। निम्न में से कोई मान लेते हैं कि मैं एक चर Autobody कहा जाता है में डेटा संग्रहीत कर रहा हूँ चलो, मैं कोशिश की है परिवर्तनः

List<char> uniqueColors = autoBody.SelectMany(auto => auto.SelectedCar.Color).Distinct().ToList(); 

मैं स्पष्ट रूप से कुछ गलत कर रहा हूँ, लेकिन मैं कैसे के लिए क्या देख रहा हूँ प्राप्त करने के लिए स्पष्ट नहीं कर रहा हूँ।

उत्तर

2

इस

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

namespace ConsoleApplication70 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      AutoBody bodies = new AutoBody() 
      { 
       vehicles = new List<Vehicles>() 
       { 
        new Vehicles() { 
         SelectedCar = new SelectedCar() { Model = "Ford", NumTires = 1, Color = "red"} 
        }, 
        new Vehicles() { 
         SelectedCar = new SelectedCar() { Model = "Chevy", NumTires = 2, Color = "blue"} 
        }, 
        new Vehicles() { 
         SelectedCar = new SelectedCar() { Model = "Jeep", NumTires = 3, Color = "green"} 
        }, 
        new Vehicles() { 
         SelectedCar = new SelectedCar() { Model = "Merecedes", NumTires = 4, Color = "red"} 
        }, 
       } 
      }; 
      List<string> colors = bodies.vehicles.Select(x => x.SelectedCar).Select(y => y.Color).Distinct().ToList(); 

     } 
    } 
    [Serializable()] 
    [System.Xml.Serialization.XmlRoot("AutoEnvelope")] 
    public class AutoBody 
    { 
     [XmlArray("AutoBody")] 
     [XmlArrayItem("Vehicles", typeof(Vehicles))] 
     public List<Vehicles> vehicles { get; set; } 

    } 

    [Serializable()] 
    public class Vehicles 
    { 
     [XmlElement("SelectedCar", typeof(SelectedCar))] 
     public SelectedCar SelectedCar { get; set; } 

     //[XmlElement("OfferedVehicles", typeof(OfferedVehicles))] 
     //public OfferedVehicles OfferedVehicles { get; set; } 

    } 

    [Serializable()] 
    public class SelectedCar 
    { 
     [System.Xml.Serialization.XmlElement("Model")] 
     public string Model { get; set; } 

     [System.Xml.Serialization.XmlElement("NumTires")] 
     public int NumTires { get; set; } 

     [System.Xml.Serialization.XmlElement("Color")] 
     public string Color { get; set; } 

    } 
} 
+0

तेज़ (और सटीक) उत्तर के लिए धन्यवाद। यह पूरी तरह से काम किया! –

5

SelectMany() विधि प्रक्षेपण एकाधिक सरणी (वास्तव में कुछ भी जो IEnumerable<T> लागू करता है) एक एकल सरणी में है।

उदाहरण के लिए, यदि आप AutoBody मदों की एक सूची होने और कर रहे थे आप एक ही सरणी में उनके संबद्ध Vehicles के सभी जमा करने के लिए चाहता था, आप क्या करेंगे:

IEnumerable<Vehicles> vehicles = autoBodies.SelectMany(x => x.Vehicles); 

लेकिन, जब तुम SelectMany उपयोग कर रहे हैं एक string संपत्ति (आपके मामले में Color) पर, आप मूल रूप से एक IEnumerable<char> (String के बाद से को लागू करता है IEnumerable<char> यह वास्तव में वर्णों का एक क्रम है, क्योंकि) में string पेश कर रहे हैं।

कोशिश Select() बजाय का उपयोग कर:

List<string> uniqueColors = autoBody.Select(auto => auto.SelectedCar.Color) 
            .Distinct() 
            .ToList() 

देखें MSDN

+0

जानकारी और विवरण के लिए धन्यवाद की कोशिश करो। –

संबंधित मुद्दे

 संबंधित मुद्दे