从嵌套对象中获取不同的值列表

时间:2016-01-19 17:58:29

标签: c# linq linq-to-objects

我将XML文件反序列化为对象模型。虽然这不是实际的模型,但下面的结构与我的相似。

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

}

我正在尝试获取SelectedCar.Color值的明确列表并且未成功。假设我将数据存储在一个名为autoBody的变量中,我尝试过以下各种变体:

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

我显然做错了什么,但我不清楚如何实现我正在寻找的东西。

2 个答案:

答案 0 :(得分:5)

SelectMany()方法用于将多个数组(实际上是实现IEnumerable<T>的任何内容)投影到单个数组中。

例如,如果您有一个AutoBody项列表,并且您想将所有关联的Vehicles累积到一个数组中,那么您可以这样做:

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

但是,当您在SelectMany属性string上使用Color时,您基本上将string投射到{{1} ()IEnumerable<char> 实现String,因为它实际上是一系列字符。)

请尝试使用IEnumerable<char>

Select()

请参阅MSDN

答案 1 :(得分: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; }

    }
}