Foreach循环没有正确输出?

时间:2016-05-19 09:43:14

标签: c# .net linq list lambda

我想按时间顺序对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循环,那将会很棒。提供的解释也很好。

3 个答案:

答案 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都与其日期相对应。