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

时间:2019-06-23 18:42:42

标签: c#

我是编程和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);
    }
}

1 个答案:

答案 0 :(得分:2)

暂时忽略代码中的问题,例如未声明的strings变量,以及将非法的0值用作{{1 }},您可能会发现这样的代码读起来更好:

DateTime