在文件中添加一行

时间:2011-08-22 20:17:44

标签: c# list add contains

我有这段代码:

List<string> lineList = new List<string>();
in j = 0;
Dictionary<string, int> dictionary = new Dictionary<string, int>();

lineList = theFinalList.Select( line =>
{
     if (line.PartDescription != "")
         return line.PartDescription + " " + line.PartNumber + " " + line.TapeWidth + "\n";
     else
         return "N/A " + line.PartNumber + " " + line.TapeWidth + "\n";
})
.Where(x => !(x.Contains("FID") || x.Contains("EXCLUDE")))
.ToList();

foreach (string word in lineList)
{
    if (dictionary.ContainsKey(word))
        dictionary[word]++;
    else
        dictionary[word] = 1;
}

var ordered = from k in dictionary.Keys
              orderby dictionary[k] descending
              select k;

foreach (string key in ordered)
{
    var splitKey = key.Split(' ');
    if (!splitKey[2].Contains("8"))
    {
        sw.WriteLine(string.Format("( {0} 0 1 1 0 0 0 0 \"\" \"\" \"\" 0 0 0 0 0 0 )", j);
        j++;
    }

    sw.WriteLine(string.Format("( {0} 0 1 1 0 0 0 0 \"{1}\" \"{2}\" \"\" {3} 0 0 0 0 0 )",
                               j, splitKey[0], splitKey[1], dictionary[key]));
    j++;
}

此输出的示例如下所示(假设所有line.TapeWidth NOT 包含“8”):

( 1 0 1 1 0 0 0 0 "2125R_1.0" "136380" "" 18 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "2125R_1.0" "128587" "" 41 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "2125R_1.0" "138409" "" 11 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "2125R_1.0" "110984" "" 8 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "3216R_1.3" "114441" "" 6 0 0 0 0 0 )

无论其

我想改变它,所以如果!line.TapeWidth.Contains("8")然后我想输出一个额外的行..让我们说包含"138409""114441"的行都有一个{ {strong> 不等于 8 。所以 NEW 输出如下所示:

TapeWidth

所以

我正在尝试插入“空白”行之前打印另一行...

有谁知道我该怎么做?

1 个答案:

答案 0 :(得分:2)

这似乎是一种简单的方法,只需在打印行之前检查条件。

foreach (string key in ordered)
{
    var splitKey = key.Split(' ');
    if (!splitKey[1].Contains("8")) {
        sw.WriteLine("( {0} 0 1 1 0 0 0 0 "" "" "" 0 0 0 0 0 )", j);
        j++;
    }
    sw.WriteLine(
        "( {0} 0 1 1 0 0 0 0 \"{1}\" \"{2}\" \"\" {3} 0 0 0 0 0 )",
        j, splitKey[0], splitKey[1], dictionary[key]);
    j++;
}