使用foreach

时间:2019-04-04 15:59:59

标签: c#

我在这里是新手,实际上对C#来说还很新。 简而言之,我通过Visual Studio使用c#,正在从数据库中调用数据,并且要将这些数据保存在.csv文件中。现在的问题是,我想同时将这些数据保存在两列中。

我的代码确实将它们写入文件中,但没有移到右行。

        Dictionary<string, string> elementNames = new Dictionary<string, string>();
        Dictionary<string, string> elementTypes = new Dictionary<string, string>();
        var nodes = webservice.nepService.GetAllElementsOfElementType(webservice.ext, "Busbar", ref elementNames, ref elementTypes);
        Dictionary<string, string> nodeResults = new Dictionary<string, string>();
        Dictionary<string, string> nodeResults1 = new Dictionary<string, string>();
        foreach (var nodename in elementNames.Values)
        {
            var nodeRes = webservice.nepService.GetResultElementByName(webservice.ext, nodename, "Busbar", -1, "LoadFlow", null);
            var Uvolt = GetXMLAttribute(nodeRes, "U");                
            nodeResults.Add(nodename, Uvolt);                          
            var Upercentage = GetXMLAttribute(nodeRes, "Up");
            nodeResults1.Add(nodename, Upercentage);
    StringBuilder strBldr = new StringBuilder();
            string outputFile = @"C:\Users\12.csv";
            string separator = ",";   

            foreach (var res in nodeResults)
            {
                strBldr.AppendLine($"{res.Key}{separator}{res.Value}");
            }

            foreach (var res1 in nodeResults1)
            {

                strBldr.AppendLine($"{separator}{separator}{res1.Value}");
            }

            File.WriteAllText(outputFile, strBldr.ToString());
        }

这是先前代码的输出: https://ibb.co/T4trQC3

我希望这些移位后的值在其他值附近移动,例如: https://ibb.co/4S25v0h 谢谢

2 个答案:

答案 0 :(得分:0)

如果您查看的是您正在使用的代码AppendLine

            strBldr.AppendLine($"{separator}{separator}{res1.Value}");

如果要在同一行上追加,请使用Append

            strBldr.Append($"{separator}{separator}{res1.Value}");

已编辑:

在linq中,您可以使用Zip函数压缩到列表

 // using System.Linq;
  var results = Results.Zip(Results1, (firstList, secondList) => firstList.Key + "," + firstList.Value + "," + secondList.Value);

编辑完整示例

public static IDictionary<string, string> Results { get; set; }
    public static IDictionary<string, string> Results1 { get; set; }


    private static void Main(string[] args)
    {

    StringBuilder strBldr = new StringBuilder();
        string outputFile = @"D:\12.csv"; 
        Results = new Dictionary<string, string>()
        {
            {"N1", "20"},
            {"N2", "0.399992"},
            {"N3", "0.369442"},
            {"N4", "0.369976"}
        };
        Results1 = new Dictionary<string, string>()
        {
            {"N1", "100"},
            {"N2", "99.9805"},
            {"N3", "92.36053"},
            {"N4", "92.49407"}
        };
        IEnumerable<string> results = Results.Zip(Results1,
            (firstList, secondList) => firstList.Key + "," + firstList.Value + "," + secondList.Value);


        foreach (string res1 in results)
        {
            strBldr.AppendLine(res1);
        }


        File.WriteAllText(outputFile, strBldr.ToString());
    }

要获得更快的代码,您可以尝试

HashSet<Tuple<string, string, string>> values = new HashSet<Tuple<string, string, string>>();

        var nodes = webservice.nepService.GetAllElementsOfElementType(webservice.ext, "Busbar", ref elementNames, ref elementTypes);

        foreach (var nodename in elementNames.Values)
        {
            var nodeRes = webservice.nepService.GetResultElementByName(webservice.ext, nodename, "Busbar", -1, "LoadFlow", null);
            var Uvolt = GetXMLAttribute(nodeRes, "U");
            var Upercentage = GetXMLAttribute(nodeRes, "Up");
            values.Add(Tuple.Create(nodename, Uvolt, Upercentage));
        } 

        var output = string.Join("\n", values.ToList().Select(tuple => $"{tuple.Item1},{tuple.Item2},{tuple.Item3}").ToList());
        string outputFile = @"C:\Users\12.csv";

        File.WriteAllText(outputFile, output); 

答案 1 :(得分:0)

如果Results和Results1的rowCount相同并且键的顺序相同,请尝试:

for (int i = 0; i < Results.Count; i++) 
   strBldr.AppendLine($"{Results[i].Key}{separator}{Results[i].Value}{separator}{Results1[i].Value}");

或者,如果行的顺序不同,请尝试:

foreach (var res in Results)
   strBldr.AppendLine($"{res.Key}{separator}{res.Value}{separator}{Results1.Single(x => x.Key == res.Key).Value}");