[英]Get Distinct List of Values from Nested Object
I am deserializing an XML file to and object model. 我将XML文件反序列化为对象模型。 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. 我试图获得一个独特的SelectedCar.Color值列表,并且不成功。 Let's assume that I am storing the data in a variable called autoBody, I have tried variations of the following: 让我们假设我将数据存储在一个名为autoBody的变量中,我尝试过以下变体:
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. 我显然做错了什么,但我不清楚如何实现我正在寻找的东西。
The SelectMany()
method is meant for projection multiple arrays (actually anything that implements IEnumerable<T>
) into a single array. SelectMany()
方法用于将多个数组(实际上是实现IEnumerable<T>
任何数组SelectMany()
投影到单个数组中。
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: 例如,如果您有一个AutoBody
项目列表,并且您想将所有关联的Vehicles
累积到一个阵列中,您可以:
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). 但是,当你使用SelectMany
在string
属性( Color
在你的情况下),你基本上投射string
入IEnumerable<char>
(因为String
确实实现IEnumerable<char>
,因为它实际上是一个字符序列)。
Try using Select()
instead: 尝试使用Select()
代替:
List<string> uniqueColors = autoBody.Select(auto => auto.SelectedCar.Color)
.Distinct()
.ToList()
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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.