获取最大绝对值及其其他相关值

getting Maximum Absolute Value and its other related value

我有一个这样的数据表

Substation      ColumnTitle     Voltage    ptime     MW
-------------------------------------------------------------
A-1                A-741          400       00:00    -23.76
A-1                A-741          400       00:01    20.54
A-1                A-741          400       00:02    -19.09
A-1                A-741          400       00:03    -13.23
A-1                A-741          400       00:04    15.27
A-1                A-741          400       00:05    -12.7
A-2                A-741          400       00:00    -33.76
A-2                A-741          400       00:01    30.54
A-2                A-741          400       00:02    -49.09
A-2                A-741          400       00:03    -23.23
A-2                A-741          400       00:04    25.27
A-2                A-741          400       00:05    -22.7

我想要 MW 字段的最大值及其相关 ptime 的所有记录。 所以我写了这个

   var highest = from e in dtReport.AsEnumerable()
                              group e by new {
                                  substation =e.Field<string>("substation"),
                                  ColumnTitle = e.Field<string>("ColumnTitle"),
                                  Voltage = e.Field<string>("Voltage")
                              }
                                  into dptgrp
                              let topsal = dptgrp.Max(x => decimal.Parse(x.Field<string>("MW")))
                              select new
                              {
                                  substation = dptgrp.Key.substation,
                                  ColumnTitle = dptgrp.Key.ColumnTitle,
                                  Voltagee = dptgrp.Key.Voltage,
                                  ptime = dptgrp.First(y => y.Field<string>("MW") == topsal.ToString()).Field<string>("ptime"),
                                  MW = topsal
                              };

问题是我想要MW绝对值的最大值。我知道我可以通过 Math.Abs() 获得绝对值。它会是这样的:

    let topsal = dptgrp.Max(x => Math.Abs(decimal.Parse(x.Field<string>("MW"))))

但在那种情况下我怎么能得到它的 ptime 的相关值,我的意思是这一行

   ptime = dptgrp.First(y => y.Field<string>("MW") == topsal.ToString()).Field<string>("ptime")

例如。对于 A-1,A741,400 ,最大值为 -23.76。我怎样才能让它的 ptime 为“00:00”?

如果我对你的问题理解正确:你想要每个变电站的所有记录包含 MW 的最大绝对值。

您应该首先按变电站分组并获取所有的最大 MW 值。使用 Substation/MW table,您可以加入原来的 table,其中 Substation 和 MW 列相同。

这工作正常。

假设这是你的数据库table实体

 class Table
    {
        public string Substation { get; set; }
        public string ColumnTitle { get; set; }
        public string ptime { get; set; }
        public string MW { get; set; }
        public string Voltage { get; set; }

    }

并且 table 包含这样的两行

 List<Table> dptgrp= new List<Table>();
        dptgrp.Add(new Table
        {
            ColumnTitle = "A-741",
            MW = "-23.76",
            ptime = "00:00",
            Substation = "",
            Voltage = "400"
        });
        dptgrp.Add(new Table
        {
            ColumnTitle = "A-741",
            MW = "20.54",
            ptime = "00:01",
            Substation = "",
            Voltage = "400"
        });

那么你可以通过下面的代码得到你想要的结果

    var maxValue = dptgrp.OrderBy(x => x.MW).FirstOrDefault().MW; // here you can use OrderByDescending  if you want to ignore the +/- signs
            var result = dptgrp.First(x => x.MW == maxValue).ptime; // this will be your final result

不确定这个答案是否足够仔细,但是每个分组中,按利息的绝对值排序并取.First(),像这样:

var listOfItemsAttainingMaxMWWithingTheirGroup = dtReport.AsEnumerable()
  .GroupBy(e => new {
    substation =e.Field<string>("substation"),
    ColumnTitle = e.Field<string>("ColumnTitle"),
    Voltage = e.Field<string>("Voltage"),
  })
  .Select(grp => grp
    .OrderByDescending(e => Math.Abs(decimal.Parse(e.Field<string>("MW"))))
    .First())
  .ToList();