我是编程新手,也是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);
}
}
https://stackoverflow.com/questions/56726846
复制相似问题