如何在linq DateTime中正确设置变量?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (7)

我是编程和C#的新手。最近,我一直在制作一个过滤csv格式数据的工具。我发现使用linq对我来说是最好的选择。正如您所看到的,本段下面发布的代码效果很好,但我想让它更灵活。我想问一下是否有更聪明的方法:DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, s.Date.Second)作为一个变量,用户可以选择它作为一个选项。现在,我编写了代码,其中用户选择选项并基于变量:PublicVariables.customfiltersetting == 5我使用条件来设置我的排序......以下是代码的其余部分:

using (var writer = new StreamWriter(outputname))
{
    var ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, s.Date.Second) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };

    if (PublicVariables.customfiltersetting == 0)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, s.Date.Second) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else if (PublicVariables.customfiltersetting == 1)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else if (PublicVariables.customfiltersetting == 2)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, 0, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else if (PublicVariables.customfiltersetting == 3)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, 0, 0, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else if (PublicVariables.customfiltersetting == 4)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, 0, 0, 0, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else if (PublicVariables.customfiltersetting == 5)
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, 0, 0, 0, 0, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }
    else
    {
        ordering = from s in Filter.FullData() group s by new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, 0) into g let count = g.Count() orderby g.Key descending select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };
    }

    foreach (var s in ordering)
    {
        Console.WriteLine(s.Date + "\t" + s.Column + "\t " + s.Average + "\t " + s.Count);
        writer.WriteLine("Date: " + strings.Value + " Value: " + strings.Average.ToString("0.00") + "\t" + " Count: " + strings.Count);
    }
}
提问于
用户回答回答于

暂时忽略代码中的问题,例如未声明的strings变量,以及使用非法0值作为月份和日期值的默认值DateTime,您可能会发现您的代码读起来有点像这样:

Func<MyDataType, DateTime> selectKey;

switch (PublicVariables.customfiltersetting)
{
    case 0:
        goto default;
    case 1:
        selectKey = s => new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, 0);
        break;
    case 2:
        selectKey = s => new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, 0, 0);
        break;
    case 3:
        selectKey = s => new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, 0, 0, 0);
        break;
    case 4:
        selectKey = s => new DateTime(s.Date.Year, s.Date.Month, 1, 0, 0, 0);
        break;
    case 5:
        selectKey = s => new DateTime(s.Date.Year, 1, 1, 0, 0, 0);
        break;
    default:
        selectKey = s => new DateTime(s.Date.Year, s.Date.Month, s.Date.Day, s.Date.Hour, s.Date.Minute, s.Date.Second);
        break;
}

var ordering = from s in Filter.FullData()
               group s by selectKey(s) into g
               let count = g.Count()
               orderby g.Key descending
               select new { Date = g.Key, Column = g.Average(s => s.Column), Count = count };

扫码关注云+社区

领取腾讯云代金券