Get a distinctive list of values ​​from a nested object

I am deserializing an XML file and an object model. Although this is not a real model, lower in structure is similar to what I have.

[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; } } 

I am trying to get a separate list of SelectedCar.Color values ​​and have failed. Suppose I store data in a variable called autoBody, I tried options for the following:

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

I obviously am doing something wrong, but I don’t understand how to achieve what I am looking for.

+5
source share
2 answers

try it

 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; } } } 
+2
source

The SelectMany() method is designed to project multiple arrays (in fact, everything that implements IEnumerable<T> ) into one array.

For example, if you have a list of AutoBody elements, and you want to copy all the Vehicles associated with it into one array, you should do:

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

But when you use SelectMany in the string property ( Color in your case), you basically project string into IEnumerable<char> (since string implements IEnumerable<char> because it is actually a sequence of characters).

Try using Select() instead:

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

See MSDN

+5
source

Source: https://habr.com/ru/post/1240947/


All Articles