如何从可枚举列表中将分组结果作为列表获取
本文关键字:列表 结果 获取 枚举 | 更新日期: 2023-09-27 18:34:16
如何从列表中获取唯一记录?
public class VinDecoded
{
public string SquishVin {get;set;} = "";
public string Year {get;set;} = "";
public string Make {get;set;} = "";
}
var modelVinDecodeds = new List<VinDecoded>();
modelVinDecodeds.Add(new VinDecoded() {SquishVin="1234",Year="2007",Make="Ford"});
modelVinDecodeds.Add(new VinDecoded() {SquishVin="2233",Year="2010",Make="Chevy"});
modelVinDecodeds.Add(new VinDecoded() {SquishVin="2233",Year="2010",Make="GMC"});
在这种情况下,我想获得只有匹配的"2233"SquishVin的自定义List<VinDecoded>()
。
我试过了,但不起作用。 我正在获得密钥但没有列表。 我只想要没有列表的List<VinDecoded>()
数据。
var modelCustomVinDecoded = modelVinDecodeds.GroupBy(x => x.SquishVin).ToList();
foreach(var v in modelCustomVinDecoded)
{
if (v.Key != "0033")
{
FooSave(v.???); //Where's the list coming from the v part?
}
}
>GroupBy(x => x.SquishVin)
将返回一个IEnumerable<IGrouping<string, VinDecoded>>
,并且IGrouping<string, VinDecoded>
具有一个名为 Key 的属性,用于返回组中所有VinDecoded
对象中的SquishVin
。此外IGrouping<string, VinDecoded>
是一个 IEnumerable<VinDecode>
,因此您可以迭代它或将其转换为列表。
您可以这样做:
var modelCustomVinDecoded = modelVinDecodeds.GroupBy(x => x.SquishVin).ToList();
foreach(var v in modelCustomVinDecoded)
{
if (v.Key != "0033")
{
FooSave(v.ToList());
}
}