0

I need to convert different tuples to lists of strings.

How can i convert a tuple that contains different types to a list of strings in C#?

And of curse i need a generic solution for different tuples.

Example:

I want to convert this tuple to a list: ("word", 1, 's', US)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Does this answer your question? [C# ValueTuple of any size](https://stackoverflow.com/questions/56413583/c-sharp-valuetuple-of-any-size) – Jacob Huckins Jun 16 '20 at 17:11
  • I suppose you could use reflection to enumerate the values of the tuple and cast them. Does this help? https://www.danielcrabtree.com/blog/365/accessing-tuples-at-runtime-using-reflection – Hal Jun 16 '20 at 17:12

4 Answers4

3

Linq solution:

 using System.Linq;
 using System.Runtime.CompilerServices;

 ...

 public static List<string> TupleToList(ITuple value) {
   if (null == value)
     throw new ArgumentNullException(nameof(value));

   return Enumerable
     .Range(0, value.Length)
     .Select(i => value[i]?.ToString()) // ?. - don't call ToString() on null
     .ToList();
 }

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Modified from Kobi Hari's answer from C# ValueTuple of any size

I added some LINQ to convert each element to a string before returning to fit the question requirements.

public List<string> TupleToStringList(ITuple tuple)
{
  var result = new List<object>(tuple.Length);
  for (int i = 0; i < tuple.Length; i++)
  {
    result.Add(tuple[i]);
  }
  return result.Select(obj => obj.ToString()).ToList();
}

Or using the original method and converting to strings as a separate step

public List<object> TupleToString(ITuple tuple)
{
  var result = new List<object>(tuple.Length);
  for (int i = 0; i < tuple.Length; i++)
  {
    result.Add(tuple[i]);
  }
  return result;
}

// your code
{
    ...
    List<object> objectList = TupleToString(tupleToConvert);
    List<string> stringList = objectList.Select(obj => obj.ToString());
}
Jacob Huckins
  • 368
  • 2
  • 15
  • i can't use the ITuple interface because it's inaccessible due to protection level. And i added using System.Runtime.CompilerServices; – Alon Ehrenlieb Jun 17 '20 at 23:33
0

Try this:

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = null;

            var t = ("word", 1, 's', "US");
            foreach (var item in TupleToList(t))
            {
                Console.WriteLine(item);
            }
        }

        private static List<string> TupleToList(ITuple tuple)
        {
            var result = new List<string>(tuple.Length);
            for (int i = 0; i < tuple.Length; i++)
            {
                result.Add(tuple[i].ToString());
            }
            return result;
        }
    }
}

The TupleToList takes a ITuple as parameter and returns list of string. Goes without saying, there could be issues based on what the tuple contains which you might need to handle.

Manish
  • 6,106
  • 19
  • 64
  • 90
  • I think it's not working for me because apparently i'm using ValueTuple and not Tuple, can you provide me the solution for valueTuple? Sorry for not mentioning it before. – Alon Ehrenlieb Jun 17 '20 at 23:32
0

Use Value Tuple

var result = return (type1, type2, type3);

return new ModelDto()
            {
                Model1= result.Item1,
                Model2= result.Item2,
                Model3= result.Item3
            };
sabuj
  • 1
  • 1