如何打印List <list <keyvaluepair>?

时间:2018-07-16 12:26:16

标签: c#

class Program
{
    static void Main(string[] args)
    {
        List<List<double>> myList = new List<List<double>>();
        myList.Add(new List<double> { 2D, 2D, 3D, 5D, 8D, 11D, 13D, 13D, 11D, 8D, 5D, 3D });
        myList.Add(new List<double> { 6D, 7D, 10D, 14D, 18D, 21D, 22D, 22D, 19D, 15D, 10D, 7D });

        List<List<KeyValuePair<string, double>>> result = new List<List<KeyValuePair<string, double>>>();

        for (int i = 0; i < myList.Count; i++)
        {
            List<KeyValuePair<string, double>> r = new List<KeyValuePair<string, double>>();

            for (int j = 0; j < myList[i].Count; j++)
            {

                r.Add(new KeyValuePair<string, double>("Values", myList[i][j]));
            }
            result.Add(r);
        }

        foreach(var element in result)
        {
            Console.WriteLine(element);
            Console.ReadLine();
        }
    }
}

我要打印列表“结果”的内容。上面的foreach循环不起作用。打印列表的正确方法是什么?

4 个答案:

答案 0 :(得分:4)

你是这个意思吗?

foreach(List<KeyValuePair<string, double>> pair in result)
{
    foreach (KeyValuePair<string, double> innerpair in pair)
    {
        Console.WriteLine(innerpair.Key + " " + innerpair.Value);
    }
}

答案 1 :(得分:4)

假设数据在内存中,则可以使用LINQ:

String.Join(Environment.NewLine, myList.SelectMany(l => l.ToString()).ToArray());

它的作用是在将值转换为字符串时,将列表列表(SelectManyToArray)变平,然后使用{{1} }。

通过在相应的部分中指定String.Join,确保可以使用LINQ。

或者,您可以使用using System.Linq;遍历foreach结果;在这种情况下,一个SelectMany就足够了。

答案 2 :(得分:1)

您应在此处使用KeyValuePair

var test = ["Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",      "Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",       "Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",       "Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",       "Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",       "Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",       "Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",       "Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",       "Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",       "Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"     ]

const locale = "en-us";
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
let test1 = test.map(function(a) { 
  let dateStr = new Date(a).toLocaleString(locale,options);
  return dateStr.replace(/(\w+), (\w+) (\d+), (\d+)/,"$1 $3 $2 $4");
})
console.log(test1)


// simpler if no replace of month:

// String

test1 = test.map(a => a.split(/ \d\d:/)[0]);
console.log(test1)

// date

test1 = test.map(a => new Date(a).toDateString());
console.log(test1)

var list = new List<KeyValuePair<string, int>>();
    list.Add(new KeyValuePair<string, double>("one", 1.00));
    list.Add(new KeyValuePair<string, double>("two, 2.00));
    list.Add(new KeyValuePair<string, double>("three", 4.00));

    foreach (var element in list)
    {
        Console.WriteLine(element);
    }

答案 3 :(得分:1)

您需要在打印到控制台的代码中嵌套另一个foreach,如下所示:

foreach(var element in result) {
       foreach(var el in element) {
                Console.WriteLine("Key: " + el.Key + ", Value: " + el.Value);
       }          
}