有人可以帮助我获取多级嵌套列表中的列表数。
我使用linq进行查询,但是无论我做什么,我都在获取顶级列表的数量。
我的班级如下
[XmlRoot(ElementName = "Map")]
public class FieldMap
{
[XmlAttribute(AttributeName = "fromPropertyName")]
public string FromPropertyName { get; set; }
[XmlAttribute(AttributeName = "toPropertyName")]
public string ToPropertyName { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "Map")]
public List<FieldMap> Map { get; set; }
}
[XmlRoot(ElementName = "Items")]
public class Items
{
[XmlElement(ElementName = "Item")]
public List<Item> Item { get; set; }
}
[XmlRoot(ElementName = "DataFieldMapper")]
public class DataFieldMapper
{
[XmlElement(ElementName = "Map")]
public List<FieldMap> Map { get; set; }
[XmlElement(ElementName = "Items")]
public Items Items { get; set; }
}
/**************************/
[XmlRoot(ElementName = "Map")]
public class FieldValue
{
[XmlAttribute(AttributeName = "propertyName")]
public string PropertyName { get; set; }
[XmlAttribute(AttributeName = "propertyValue")]
public string PropertyValue { get; set; }
}
[XmlRoot(ElementName = "ItemValue")]
public class ItemValue
{
[XmlElement(ElementName = "Map")]
public List<FieldValue> Map { get; set; }
}
[XmlRoot(ElementName = "Items")]
public class ItemValues
{
[XmlElement(ElementName = "ItemValue")]
public List<ItemValue> ItemValue { get; set; }
}
[XmlRoot(ElementName = "GenericModel")]
public class GenericModel
{
[XmlElement(ElementName = "Map")]
public List<FieldValue> Map { get; set; }
[XmlElement(ElementName = "Items")]
public ItemValues Items { get; set; }
}
[XmlRoot(ElementName = "Collection")]
public class GenericModelCollection
{
[XmlElement(ElementName = "GenericModel")]
public List<GenericModel> GenericModel { get; set; }
}
我正在尝试做
int count = dict
.GenericModel
.Select(x => x.Items.ItemValue)
.Select(p => p.Select(h => h.Map))
.Count();
我总是最终获得最高级别的计数
答案 0 :(得分:3)
您需要整理列表以获取模型中所有FieldValue
实例的数量:
int count = dict
.GenericModel
.SelectMany(x => x.Items.ItemValue)
.SelectMany(h => h.Map)
.Count();