得到最大绝对值和其他相关值

时间:2019-07-13 07:07:49

标签: c# linq visual-studio-2010

我有一个这样的数据表

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,最大Vlaue为-23.76。我该如何设置其ptime为“ 00:00”?

3 个答案:

答案 0 :(得分:1)

不确定此答案是否足够谨慎,但是在每个分组中 内,请按兴趣的绝对值排序并采用.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();

答案 1 :(得分:0)

如果我正确理解了您的问题:您希望每个变电站的所有记录都包含MW的最大绝对值。

您应该首先按变电站分组并获取所有组件的最大MW值。使用Substation / MW表,您可以将原始表与Substation和MW列相同的表连接起来。

答案 2 :(得分:0)

这很好。

假设这是您的数据库表实体

 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; }

    }

该表包含这样的两行

 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