Create list with more than one element as if it were multidimensional

4

I need to receive these pairs of data from the database, and I'm thinking of a way to bring the values without having to create an object, as I find it unnecessary in this case.

Is it possible to create a list<string,string> or something like this? so I can access list[0].value1 and list[0].value2 . I know there is Dictionary , but it's not the solution I'm looking for.

    
asked by anonymous 13.08.2018 / 16:53

4 answers

7

The two solutions presented are bad, one creates a class of class and allocates as crazy being inefficient in memory and processing and another renders the element as a dictionary that does not have the same semantics of a list of lists, or even what you want, so you're talking, because what you want is a list of objects, not lists.

The right solution for what you want (by what you've demonstrated and not by what you've said) has a chance of creating a struct that takes care of it, something like this:

struct Item { public string AdminId, public string GroupId }
var lista = new List<Item>() { new Item { var1, var2 }, };

I used the name Item because I do not know what this is about, it should be a better name. I did it in a very simplified way, it would probably be better to do something more complete than this for several reasons.

On the other hand this might even be something fleeting and should not create a type to administer this, which I could only know if I had more details of the application. Here a tuple would be more appropriate, but it would be the modern tuple of C # 7. Something like this:

var lista = new List<(string AdminId, string GroupId)>() { (var1, var2), };

This var1 and var2 are local variables with the content, it does not have to be fine, if the question had the code to create I would do a more detailed example. I used to show with os and initialize the code, and of course I would have to use several items. It might be more interesting to boot into a loop later:

foreach (var item in fonteDeDados) lista.Add((item.AdminId, item.GroupId));

Or using the structure:

foreach (var item in fonteDeDados) lista.Add(new Item() { item.AdminId, item.GroupId });

Of course, I'm considering that the data source that will build your list comes from an object that has the fields or properties with the names I used.

    
13.08.2018 / 17:57
6

By your description you would like to use the Tuple<T1, T2> structure, here is an example of what you described you need:

List<Tuple<string, string>> tupla = new List<Tuple<string, string>>();

tupla.Add(new Tuple<string, string>("valor1", "valor2"));
tupla.Add(new Tuple<string, string>("valor3", "valor4"));

//Acessando o primeiro item da lista
string item1 = tupla[0].Item1;
string item2 = tupla[0].Item2;
    
13.08.2018 / 17:01
1

Perhaps the most viable to create a list will be Dictionary<string, string> , which creates a list of KeyValuePair .

Dictionary<string, string> dictionary = new Dictionary<string, string>();

foreach (var item in dictionary)
    Console.WriteLine($"Key = {item.Key}, Value = {item.Value}");
    
13.08.2018 / 17:02
0

I really like the Tuple class, see example:

//Definindo o valor com a inicialização de coleção simplificada
List<(string ADMIN_ID, string USER_GROUP_ID)> tuples = new List<(string, string)>
{
    ("3984", "3985"),
    ("3599", "3773")
};

//Outra forma de definir o valor
List<(string ADMIN_ID, string USER_GROUP_ID)> tuples1 = new List<(string, string)>();
tuples1.Add(("3984", "3985"));
tuples1.Add(("3599", "3773"));

//Obtendo o valor
string item1 = tuples.ElementAt(0).ADMIN_ID; //ou tuples[0].ADMIN_ID;
string item2 = tuples.ElementAt(0).USER_GROUP_ID; // ou tuples[0].USER_GROUP_ID;

//Outra forma de obter o valor
(string ADMIN_ID, string USER_GROUP_ID ) = tuples.ElementAt(0); // ou tuples[0];

Source : link

Nuget : link

  

Remembering that the C # version has to be 7.0

    
14.08.2018 / 16:29