强类型 List.GroupBy()

发布于 2024-12-22 23:47:40 字数 1092 浏览 1 评论 0原文

我一直试图在谷歌和SO上找到这个问题的答案

,但我发现的所有地方都使用匿名类型的结果列表,

我想要实现的是采用 List

并创建一个 每个SecondaryStandardSecondaryStandard 分组列表

看起来像这样,

public class SecondaryStandard
{

    public string Id { get; set; }
    public int IdNumeric { get; set; }
    public string IdText { get; set; }
    public Sample Sample { get; set; }
    public string StandardName { get; set; }
    public DateTime DateCompleted { get; set; }
    public SamplePoint SamplingPoint{ get; set; }
    public Instrument Instrument{ get; set; }
    public string ContainerId { get; set; }
    public double Value { get; set; }
    public string ComponentName { get; set; }
    public string PointLocation { get; set; }
    public string Description { get; set; }
    public string Description2 { get; set; }
    public string Analysis { get; set; }
    public string Units { get; set; }

}

我想要的是一个 List(),其中 Value 属性是每个的结果的平均值组件名称.

关于如何以强类型的方式实现这一点的任何想法,或者我是否需要吸收它并使用匿名对象来实现我正在寻找的东西?

I've been trying to find an answer to this on google and on SO

but everywhere I have found uses anonymously typed result lists

what I am trying to acheive is to take a List<SecondaryStandard>

and create a grouped List of SecondaryStandard

each SecondaryStandard looks like this

public class SecondaryStandard
{

    public string Id { get; set; }
    public int IdNumeric { get; set; }
    public string IdText { get; set; }
    public Sample Sample { get; set; }
    public string StandardName { get; set; }
    public DateTime DateCompleted { get; set; }
    public SamplePoint SamplingPoint{ get; set; }
    public Instrument Instrument{ get; set; }
    public string ContainerId { get; set; }
    public double Value { get; set; }
    public string ComponentName { get; set; }
    public string PointLocation { get; set; }
    public string Description { get; set; }
    public string Description2 { get; set; }
    public string Analysis { get; set; }
    public string Units { get; set; }

}

what i want is a List() where the Value Property is an average of results for each ComponentName.

Any ideas on how I could achieve this in a strongly typed way or do I need to suck it up and use anonymous objects to achieve what I'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-12-29 23:47:40

你是说这个吗?

List<SecondaryStandard> list = new List<SecondaryStandard>();
// populate list

List<SecondaryStandard> result = list
    .GroupBy(l => l.ComponentName)
    .Select(s => new SecondaryStandard() { ComponentName = s.Key, Value = s.Average(x => x.Value) }).ToList();

You mean this?

List<SecondaryStandard> list = new List<SecondaryStandard>();
// populate list

List<SecondaryStandard> result = list
    .GroupBy(l => l.ComponentName)
    .Select(s => new SecondaryStandard() { ComponentName = s.Key, Value = s.Average(x => x.Value) }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文