Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Distinct List of Values from Nested Object

I am deserializing an XML file to and object model. Although this is not the actual model, below is similar in structure 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 distinct list of SelectedCar.Color values and have been unsuccessful. Let's assume that I am storing the data in a variable called autoBody, I have tried variations of the following:

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

I am clearly doing something wrong, but am not clear on how to achieve what I am looking for.

like image 860
Todd Zetlan Avatar asked Jan 19 '16 17:01

Todd Zetlan


2 Answers

The SelectMany() method is meant for projection multiple arrays (actually anything that implements IEnumerable<T>) into a single array.

For example, if you were having a list of AutoBody items and you wanted to accumulate all of their associated Vehicles into a single array, you would do:

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

But, when you're using SelectMany on a string property (Color in your case), you're basically projecting the string into an IEnumerable<char> (since String does implement IEnumerable<char> because it's actually a sequence of characters).

Try using Select() instead:

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

See MSDN

like image 90
haim770 Avatar answered Oct 18 '22 02:10

haim770


Try this

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

    }
}
like image 44
jdweng Avatar answered Oct 18 '22 04:10

jdweng