【问题标题】:List filter based on date基于日期的列表过滤器
【发布时间】:2013-09-06 15:46:24
【问题描述】:

我有这样的情况。我有一个列表,其中包含日期和时间的字符串,格式如下:05-04-2011 12:42:03.199。我希望能够根据在 datetimepicker 和我的自定义 timepicker 中选择的日期来过滤这个列表。我将这两个值都保存到字符串string try1 = timePicker1.Value.ToString();string try2 = dateTimePicker5.Value.ToString("dd-MM-yyyy") 中。我已经用view 完成了过滤,如下所示:

view2.RowFilter = "TimeColumn >= '" 
                  + dateTimePicker5.Value.ToString("dd-MM-yyyy") + " " 
                  + try1 
                  + "' and TimeColumn <= '" 
                  + dateTimePicker4.Value.ToString("dd-MM-yyyy") + " " 
                  + try2 + "'";

我想知道如何使用 linq 执行此操作并将结果保存到新列表?是否可以通过从顶部和底部的减少来过滤它?

希望我说得很清楚。不只是告诉我,我会尽力解释更多!

【问题讨论】:

  • 为什么将DateTime 存储为String?为什么你有一个包含日期的List&lt;String&gt;
  • @TimSchmelter 我认为这比他的过滤问题更重要..
  • 我这样做是因为我的主线不仅包含这些数据。我首先将它分成 3 部分并保存到字符串数组中。它里面有数据和大量的文本。我怎样才能以不同的方式做到这一点?

标签: c# winforms linq filtering


【解决方案1】:

为什么将DateTime 存储为String?为什么你有一个包含DateTimes 的List&lt;String&gt;?您应该始终使用最合适的数据类型并最后将其转换为字符串。

但是,这应该可以:

string dateFormat = "dd-MM-yyyy HH:mm:ss.fff";
IEnumerable<string> filtered = strings
    .Select(str => new { str, dt = DateTime.ParseExact(str, dateFormat, CultureInfo.InvariantCulture) })
    .Where(x => x.dt >= start && x.dt <= end)
    .OrderBy(x => x.dt) // if you want to order by the datetime value
    .Select(x => x.str); 

此查询获取每个字符串并使用DateTime.ParseExact 将其转换为DateTime。然后它创建一个匿名类型(new{ .... }),它有两个属性,原始的stringDateTime,并通过DateTime 属性和你的开始值和结束值过滤Enumerable.Where。剩余项目按日期时间(奖金)排序。最后选择了字符串,因为我假设这是您想要的结果。

如果您需要具体化查询,您可以使用ToList 或简单的foreach

foreach(string strDate in filtered)
{
    Console.WriteLine( strDate );
}

编辑根据您的评论:

好的,那么当我的主行看起来像时,如何将其保存为日期时间 这 05-04-2011 12:42:04.160,事件 dssd(23).dfgg[2] 1 xx:3332,一些 更多文字。我像这样分隔这些行并保存每个部分 进入字符串数组 string[] _columns = line.Split(",".ToCharArray());。 如何将第一部分保存为日期时间,其余部分保存为字符串?

string dateFormat = "dd-MM-yyyy HH:mm:ss.fff";
string[] lines = new[]{"05-04-2011 12:42:04.160,Event dssd(23).dfgg[2] 1 xx:3332,some more text"};
var lineIfos = lines.Select(s => s.Split(','))
    .Select(split => new{ 
        date = DateTime.ParseExact(split[0].Trim(), dateFormat, CultureInfo.InvariantCulture),
        rest = string.Join(",", split.Skip(1))
    })
    .Where(x => x.date >= start && x.date <= end)
    .OrderBy(x => x.date);

foreach (var x in lineIfos)
    Console.WriteLine("date:{0}  rest of the line:{1}"
        ,x.date.ToString("d")
        ,x.rest);

【讨论】:

  • 你能解释一下你的代码吗,我不太明白。对不起。
  • 我这样做了,但我仍然得到错误,这是我的代码:'IEnumerable filtered = list3.Select(str => new { str.LeftColumn, dt = DateTime.ParseExact (str.LeftColumn, dateFormat, CultureInfo.InvariantCulture) }).Where(x => x.dt >= time1 && x.dt x.dt).Select(x => x .str);'
  • 所以LeftColum 是自定义类型的属性,它是实际上是DateTime-string 的字符串?您必须在匿名类型中指定属性的名称或存储变量本身,我重命名它,因为它不是字符串而是对象。 list3.Select(obj =&gt; new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) }).Where....Select(x =&gt; x.obj)
  • 我更正了它,但仍然存在一个问题,即我来自 datetimepicker 的值和来自 timepicker 的这个 try 值是字符串,当我尝试解析它时,我得到错误,它不是正确的格式。 ..
  • 我已经用你的示例字符串检查了它并且它有效。因此,您需要提供可能的日期时间字符串的完整列表(您会发现使用DateTimes 更好,它更高效且不易出错)。如果无法转换字符串,您可能希望使用DateTime.TryParseExact 来排除无效字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
相关资源
最近更新 更多