LINQ GroupBy并选择其他属性值的列表

huangapple go评论63阅读模式
英文:

LINQ GroupBy and select a list of other property values

问题

我有这样的配对:

public class AnalyteUnitPair
{
    public string Analyte { get; set; }
    public string Unit { get; set; }
}

我需要将这些列表重新组合成如下所示的分组列表:

现有内容:

Analyte ="Ce", Unit="g"
Analyte ="Ce", Unit="%"
Analyte ="Ce", Unit="ppm"
Analyte ="Dy", Unit="kg"
Analyte ="Dy", Unit="%"
Analyte ="Dy", Unit="ppm"

我需要将上面的列表转换为对象列表:

public class AnalyteUnits
{
    public string Analyte { get; set; }
    public string Units { get; set; }
}

看起来像这样:

Analyte="Ce", Units="g:g,%:%,ppm:ppm"
Analyte="Dy", Units="kg:kg,%:%,ppm:ppm"

请不要建议使用字典,我不想在JS端处理它们。

英文:

I have pairs as such:

public class AnalyteUnitPair
	{
		public string Analyte { get; set; }
		public string Unit { get; set; }
	}

and I need to turn the list of these into a reshuffled grouped by as such:

what I have

Analyte ="Ce", Unit="g"
Analyte ="Ce", Unit="%"
Analyte ="Ce", Unit="ppm"
Analyte ="Dy", Unit="kg"
Analyte ="Dy", Unit="%"
Analyte ="Dy", Unit="ppm"

what I need is to convert the list above into the list of objects

public class AnalyteUnits
	{
		public string Analyte { get; set; }
		public string Units { get; set; }

	}

that would look like this

Analyte="Ce", Units="g:g,%:%,ppm:ppm";
Analyte="Dy", Units="kg:kg,%:%,ppm:ppm";

Please do not suggest Dictionary, I don't want to deal with them on the JS side.

答案1

得分: 1

以下是翻译好的部分:

var listOfPairs = new List<AnalyteUnitPair>
{
    new AnalyteUnitPair { Analyte = "Ce",Unit = "g" },
    new AnalyteUnitPair { Analyte = "Ce",Unit = "%" },
    new AnalyteUnitPair { Analyte = "Ce",Unit = "ppm" },
    new AnalyteUnitPair { Analyte = "Dy",Unit = "kg" },
    new AnalyteUnitPair { Analyte = "Dy",Unit = "%" },
    new AnalyteUnitPair { Analyte = "Dy",Unit = "ppm" }
};

var analyteUnitsQuery =
    from pair in listOfPairs
    group pair by pair.Analyte into g
    select new AnalyteUnits
    {
        Analyte = g.Key,
        Units = string.Join(',', g.Select(pair => $"{pair.Unit}:{pair.Unit}"))
    };

foreach (var analyteUnits in analyteUnitsQuery)
{
    Console.WriteLine($"Analyte={analyteUnits.Analyte}, Units={analyteUnits.Units}");
}

public class AnalyteUnitPair
{
    public string Analyte { get; set; }
    public string Unit { get; set; }
}

public class AnalyteUnits
{
    public string Analyte { get; set; }
    public string Units { get; set; }
}
英文:

How's this:

var listOfPairs = new List&lt;AnalyteUnitPair&gt;
{
    new AnalyteUnitPair { Analyte = &quot;Ce&quot;,Unit = &quot;g&quot; },
    new AnalyteUnitPair { Analyte = &quot;Ce&quot;,Unit = &quot;%&quot;},
    new AnalyteUnitPair { Analyte = &quot;Ce&quot;,Unit = &quot;ppm&quot;},
    new AnalyteUnitPair { Analyte = &quot;Dy&quot;,Unit = &quot;kg&quot;},
    new AnalyteUnitPair { Analyte = &quot;Dy&quot;,Unit = &quot;%&quot;},
    new AnalyteUnitPair { Analyte = &quot;Dy&quot;,Unit = &quot;ppm&quot;}
};

var analyteUnitsQuery =
    from pair in listOfPairs
    group pair by pair.Analyte into g
    select new AnalyteUnits
    {
        Analyte = g.Key,
        Units = string.Join(&#39;,&#39;, g.Select(pair =&gt; $&quot;{pair.Unit}:{pair.Unit}&quot;))
    };

foreach (var analyteUnits in analyteUnitsQuery)
{
    Console.WriteLine($&quot;Analyte={analyteUnits.Analyte}, Units={analyteUnits.Units}&quot;);
}

public class AnalyteUnitPair
{
    public string Analyte { get; set; }
    public string Unit { get; set; }
}

public class AnalyteUnits
{
    public string Analyte { get; set; }
    public string Units { get; set; }
}

答案2

得分: 1

首先按照 Analyte 进行分组,然后你可以选择每个分组的所有 Units

var condensed = originalList
    .GroupBy(item => item.Analyte)
    .Select(group => new AnalyteUnitPair()
    {
        Analyte = group.Key,
        Unit = string.Join(",", group.Select(item => $"{item.Unit}:{item.Unit}"))
    });
英文:

First group by the Analyte, then you can select all the Units for each group.

var condensed = originalList
    .GroupBy(item =&gt; item.Analyte)
    .Select(group =&gt; new AnalyteUnitPair()
    {
        Analyte = group.Key,
        Unit = string.Join(&quot;,&quot;, group.Select(item =&gt; $&quot;{item.Unit}:{item.Unit}&quot;))
    });

huangapple
  • 本文由 发表于 2023年7月11日 03:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定