我想按时间顺序对csv文件进行排序,这是我的代码:
userRoleAssignedTo
但是,它不仅不按时间顺序对文件进行排序,而且for (int i = 0; i < line.Length; i++)
{
string bgColour = "";
string[] col = line[i].Split(new string[] { "," }, StringSplitOptions.None);
csvList.Add(new CSVEntry() { date = DateTime.ParseExact(col[7], "dd/MM/yyyy", null) });
var dates = csvList.OrderBy(x => x.date).Select(x => x.date);
foreach (DateTime dueDate in dates)
{
if (dueDate == DateTime.Today)
{
// if due date is today, blue
bgColour = "40FFFF";
}
else if (dueDate > DateTime.Today)
{
// if due date is not today yet, white
bgColour = "FFFFFF";
}
else
{
// if due date has passed, yellow
bgColour = "FFFF40";
}
}
}
的颜色也不能准确显示。仅显示dueDate
。
我对yellow
循环知之甚少,不熟悉lambda,foreach
和LINQ。
编辑#1:
我尝试将List<T>
循环置于foreach
循环之外,就像下面的一些解决方案一样,但for
似乎没有在其后工作。
以下代码集是我想要执行的内容的延续:
bgColour
所以,我对此不确定,但for (int i = 0; i < line.Length; i++)
{
string[] col = line[i].Split(new string[] { "," }, StringSplitOptions.None);
... (All the array and foreach codes from above...)
if (col[1] == "Open")
{
if (col[9] == "DEFAULT")
{
sb1.AppendLine(new1 + bgColour + new2 + bgColour + new3);
Label1.Text = sb1.ToString();
}
}
}
必须在bgColour
之前,对吧? AppendLine
位于AppendLine
循环内。那我怎么解决这个问题呢?
编辑#2:
我找到了for
使用这组代码的另一种方法:
bgColour
基于this之类的某些来源,它声明for (int i = 0; i < line.Length; i++)
{
string[] col = line[i].Split(new string[] { "," }, StringSplitOptions.None);
string[] dates = col[7].ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Array.Sort(dates); // I know there's a problem here, please see below.
foreach (string date in dates)
{
DateTime dueDate = DateTime.ParseExact(date, "dd/MM/yyyy", null);
if (dueDate == DateTime.Today)
{
// if due date is today, blue
bgColour = "40FFFF";
}
else if (dueDate > DateTime.Today)
{
// if due date is not today yet, white
bgColour = "FFFFFF";
}
else
{
// if due date has passed, yellow
bgColour = "FFFF40";
}
}
if (col[1] == "Open")
{
if (col[9] == "DEFAULT")
{
sb1.AppendLine(new1 + bgColour + new2 + bgColour + new3);
Label1.Text = sb1.ToString();
}
}
}
可以正常使用Array.Sort()
数组,因此我决定对其进行测试,但它对我的工作不起作用码。但是,这次DateTime
已经到位。
我知道我的bgColour
循环在foreach
循环中仍然,这不应该是这种情况。如果有人可以帮我解决for
循环,那将会很棒。提供的解释也很好。
答案 0 :(得分:0)
您正在为csv文件中的每一行执行排序和输出。您需要首先构建整个csvList,然后运行排序和输出。
string bgColour = "";
for (int i = 0; i < line.Length; i++)
{
string[] col = line[i].Split(new string[] { "," }, StringSplitOptions.None);
csvList.Add(new CSVEntry() { date = DateTime.ParseExact(col[7], "dd/MM/yyyy", null) });
}
var dates = csvList.OrderBy(x => x.date).Select(x => x.date);
foreach (DateTime dueDate in dates)
{
if (dueDate == DateTime.Today)
{
// if due date is today, blue
bgColour = "40FFFF";
}
else if (dueDate > DateTime.Today)
{
// if due date is not today yet, white
bgColour = "FFFFFF";
}
else
{
// if due date has passed, yellow
bgColour = "FFFF40";
}
}
答案 1 :(得分:0)
for (int i = 0; i < line.Length; i++)
{
string bgColour = "";
string[] col = line[i].Split(new string[] { "," }, StringSplitOptions.None);
csvList.Add(new CSVEntry() { date = DateTime.ParseExact(col[7], "dd/MM/yyyy", null) });
}
var dates = csvList.OrderBy(x => x.date).ToList();
foreach (DateTime dueDate in dates)
{
if (dueDate == DateTime.Today)
{
// if due date is today, blue
bgColour = "40FFFF";
}
else if (dueDate > DateTime.Today)
{
// if due date is not today yet, white
bgColour = "FFFFFF";
}
else
{
// if due date has passed, yellow
bgColour = "FFFF40";
}
}
答案 2 :(得分:0)
我可能错了,但我想你有这样的事情:
public class CSVEntry
{
public string bgColor { get; set;}
public DateTime date { get; set; }
}
因此,您可以使用以下功能轻松解析CSV:
List<CSVEntry> csvList = line.Select(l => l.Split(',')).
Select(col => {
CSVEntry entry = new CSVEntry { date = DateTime.ParseExact(col[7], "dd/MM/yyyy", null)};
if (entry.date == DateTime.Today)
date.bgColour = "40FFFF";
else if (entry.date > DateTime.Today)
date.bgColour = "FFFFFF";
else
date.bgColour = "FFFF40";
return entry;}).
OrderBy(csventry => csventry.date).ToList();
现在,您有一个按CSVEntry
排序的date
列表,每个条目的bgColor
都与其日期相对应。