英文:
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<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; }
}
答案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 => item.Analyte)
.Select(group => new AnalyteUnitPair()
{
Analyte = group.Key,
Unit = string.Join(",", group.Select(item => $"{item.Unit}:{item.Unit}"))
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论