简体   繁体   English

如何从可枚举列表中以列表的形式获取分组结果?

[英]How to get grouping result as a list from the enumerable list?

How do I get unique records from list? 如何从列表中获得唯一记录?

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"});

In this case, I want to get custom List<VinDecoded>() having only the matching SquishVin of "2233". 在这种情况下,我想获得仅具有匹配的SquishVin“ 2233”的自定义List<VinDecoded>()

This I tried which don't work. 我尝试了这不起作用。 I'm getting Key and no List. 我收到密钥,没有列表。 I only want the List<VinDecoded>() data where there's no List. 我只想要没有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) will returns an IEnumerable<IGrouping<string, VinDecoded>> and IGrouping<string, VinDecoded> have a property called Key returning the SquishVin in all VinDecoded objects in the group. GroupBy(x => x.SquishVin)将返回IEnumerable<IGrouping<string, VinDecoded>>IGrouping<string, VinDecoded>具有名为Key的属性IGrouping<string, VinDecoded>该属性返回SquishVin中所有VinDecoded对象中的VinDecoded Also IGrouping<string, VinDecoded> is an IEnumerable<VinDecode> , so you can iterate it or just convert it to a list. 另外, IGrouping<string, VinDecoded>IEnumerable<VinDecode> ,因此您可以对其进行迭代或将其转换为列表。

You can do this: 你可以这样做:

var modelCustomVinDecoded = modelVinDecodeds.GroupBy(x => x.SquishVin).ToList();

foreach(var v in modelCustomVinDecoded)
{
    if (v.Key != "0033")
    {
        FooSave(v.ToList());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM