LINQ for groupby然后根据最短日期查找值

时间:2013-01-08 01:08:31

标签: c# linq

我有一些我要解析的数据然后根据它,我必须获取每个组的最小数据。

数据文件如下(一小部分)

D_REPDATE;INDEX;MATLABEL;ST_START;ST_END;QT_START;QT_END;PRC_RF0;PRC_RF1
APR-13;04/01/13;F35MEDFC;APR-13;01/04/13;30/04/13;01/04/11;29/03/13;596.02000;596.02000
APR-14;04/01/13;F35MEDFC;APR-14;01/04/14;30/04/14;30/03/12;31/03/14;581.77000;581.77000
APR-15;04/01/13;F35MEDFC;APR-15;01/04/15;30/04/15;01/04/13;31/03/15;567.27000;567.27000
APR-16;04/01/13;F35MEDFC;APR-16;01/04/16;30/04/16;01/04/14;31/03/16;556.52000;556.52000
APR-17;04/01/13;F35MEDFC;APR-17;01/04/17;30/04/17;01/04/15;31/03/17;548.77000;548.77000
APR-13;04/01/13;BR DATED;APR-13;01/04/13;30/04/13;01/04/11;29/03/13;108.46000;108.46000
APR-14;04/01/13;BR DATED;APR-14;01/04/14;30/04/14;30/03/12;31/03/14;102.53000;102.53000
APR-15;04/01/13;BR DATED;APR-15;01/04/15;30/04/15;01/04/13;31/03/15;98.19000;98.19000
APR-16;04/01/13;BR DATED;APR-16;01/04/16;30/04/16;01/04/14;31/03/16;94.49000;94.49000
JAN-21;04/01/13;F630GCW;JAN-21;01/01/21;31/01/21;01/01/19;31/12/20;85.57000;85.57000
JUL-13;04/01/13;F630GCW;JUL-13;01/07/13;31/07/13;01/07/11;28/06/13;93.50000;93.50000
JUL-14;04/01/13;F630GCW;JUL-14;01/07/14;31/07/14;29/06/12;30/06/14;90.95000;90.95000
JUL-15;04/01/13;F630GCW;JUL-15;01/07/15;31/07/15;01/07/13;30/06/15;88.83000;88.83000
JUL-16;04/01/13;F630GCW;JUL-16;01/07/16;31/07/16;01/07/14;30/06/16;87.01000;87.01000

对于最小QT_END的每个索引,我想读取PRC_RF0。

我写过类似

的内容
class GMRTest    
{
    public string CRVLABEL;
    public string INDEX;
    public string MATLABEL;
    public DateTime ST_START;
    public DateTime ST_END;
    public DateTime QT_START;
    public DateTime QT_END;
    public string PRC_RF0;
    public string PRC_RF1;
}

并将此方法读入列表(listData)

listData.GroupBy(s => s.INDEX).Select( aa => new {Key = aa.Key, Data = aa.OrderBy(ab => ab.ST_START) } );

然而,这仍然是给我所有的数据,我想要的是具有最小日期的数据(ST_START)。

感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:3)

尝试在First()来电后附加OrderBy。例如:

var result = listData.
    GroupBy(s => s.INDEX).
    Select(group => new {Key = group.Key, Data = group.
        OrderBy(ab => ab.ST_START).
        First()});