0

我有以下格式的数据列表,

TF000040070004-29/08/12
TF000040070004-29/08/12
TF000040070005-29/08/12
TF000020010004-29/08/12
TF000020010007-29/08/12
TF000020010002-29/08/12
TF000010010012-29/08/12
TF000010010014-29/08/12
TF000010010014-29/08/12
TF000010010005-29/08/12
TF000010010005-29/08/12
TF000010010006-29/08/12
TF000010010002-29/08/12
TF000010010008-29/08/12
TF000010010008-29/08/12
TF000010010008-29/08/12
TF000010010008-29/08/12

我使用以下 linq 查询来获取计数、键值以及最小值和最大值。现在我想选择特定日期的数据。

var serialNumbers = from sn in code
                    group sn by sn.Substring(0, 10) into g
                    select new { Key = g.Key, 
                                 Cnt = g.Count(), 
                                 Min = g.Min(v => v.Substring(10)), 
                                 Max = g.Max(v => v.Substring(10)) };
4

3 回答 3

1

尝试以下操作:

from sn in rawData where sn.Substring(15, 8) == "29/08/12"
                   select sn;

然后,您可以将您的 linq 查询(group by等)附加到...

于 2012-09-03T08:50:06.033 回答
0

如果字符串末尾没有有效的 DateTime,这将失败...

var selectionDate = new DateTime(2012,8,29);

var serialNumbers = from sn in code 
            where DateTime.Parse(
                         sn.Trim().Substring(sn.Trim().Length-8,8), 
                         new DateTimeFormatInfo {ShortDatePattern="dd/MM/yy"}) == 
                              selectionDate;
             group sn by sn.Substring(0, 10) into g 
             select new { Key = g.Key,  
                          Cnt = g.Count(),  
                          Min = g.Min(v => v.Substring(10)),  
                          Max = g.Max(v => v.Substring(10)) }; 
于 2012-09-03T08:54:37.183 回答
0

首先,您需要转换数据以便更好地操作。

var transformed = code.Select(item=>
{
    var parts = item.Split(new string[]{"-"}, StringSplitOptions.None);
    var keyPart = parts[0].Substring(2);
    var key = long.Parse(keyPart);
    var date = DateTime.Parse(parts[1]);
    return new KeyValuePair<long, DateTime>(key, date);
}

转换后,对强类型数据执行所需的查询:

var date = DateTime.Today; // your date here
var result = transformed.Where(item => item.Value == date);
于 2012-09-03T08:59:53.583 回答