我有一个XML文件:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Cars>
<Car>
<Id>1</Id>
<Color>Blue</Color>
<Price>2000</Price>
</Car>
<Car>
<Id>2</Id>
<Color>Blue</Color>
<Price>2000</Price>
</Car>
<Car>
<Id>3</Id>
<Color>Blue</Color>
<Price>3000</Price>
</Car>
<Car>
<Id>4</Id>
<Color>Green</Color>
<Price>2000</Price>
</Car>
<Car>
<Id>5</Id>
<Color>Green</Color>
<Price>3000</Price>
</Car>
</Cars>
以及相关的课程:
[XmlRoot(ElementName = "Cars")]
public class Cars
{
[XmlElement(ElementName = "Car")]
public List<Car> Car { get; set; }
}
public class Car
{
public string Id { get; set; }
public string Color { get; set; }
public string Price { get; set; }
}
我想按照颜色和价格对汽车进行分组,并有一个包含组结果的列表。
我做:
Cars cars = Dezerialise<Cars>(MyXmlFile);
var group =
from c in d.Car
group c by new
{
c.Color,
c.Price,
} into gcs
select new Car()
{
Color = gcs.Key.Color,
Price = gcs.Key.Price,
};
我希望在我的结果的每个索引中都有一个汽车匹配列表:group [0] =有两辆车的列表,组1 =有一辆车的列表等等