簡體   English   中英

如何從可枚舉列表中以列表的形式獲取分組結果?

[英]How to get grouping result as a list from the enumerable 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"});

在這種情況下,我想獲得僅具有匹配的SquishVin“ 2233”的自定義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的屬性IGrouping<string, VinDecoded>該屬性返回SquishVin中所有VinDecoded對象中的VinDecoded 另外, 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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM