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

Asked

Viewed 283 times

5

I need to receive these pairs of data from the bank, and I’m thinking of a way to bring the values without creating an object, because I think it’s unnecessary in this case.

Dados a serem importados

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

4 answers

7


The two solutions presented are bad, one creates a class class and allocates like crazy being inefficient in memory and processing and another makes the element like a dictionary that doesn’t have the same semantics of a list of lists, or even what you want, by what you’re saying, because what you want is a list of objects and not lists.

The appropriate solution for what you want (for what you have shown and not for what you have said) has a chance to be the creation of a struct take care of it, something like that:

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

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

On the other hand, this may be something fleeting and I shouldn’t create a type to manage it, which I could only know if I had more details of the application. Then a tuple would be more suitable, but it would be the modern tuple of C# 7. Something like this:

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

That one var1 and var2 are local variables with the content, it doesn’t have to be that well, if the question had the code to create I would make a more detailed example. I took the opportunity to show how to initialize the code, and of course I would have to use several items. It may be more interesting to initialize later in a loop:

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 });

I put in the Github for future reference.

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

  • take a look at the title, I decided not to edit back, I think you got confused.

  • 1

    Does it say the lack of a letter or something? But you can fix it, the question is yours, I just gave an improved to indicate better what it is, I was very

  • @Maniero ended up answering mine, I saw that already has an example : https://answall.com/questions/94882/o-que-é-tuple-e-quando-use

  • Is not repeated ?

  • @Matheusmiranda thought that not because it’s a specific applicability, you thought?

  • @Maniero I think so, because you already have the answers as Tuple and Dictionary both do the same thing Leonardo but he didn’t know how to do Dictionary. So you end up giving the answers as Tuples.

Show 1 more comment

6

By your description you would like to use the structure Tuple<T1, T2>, I’ll leave below an example of what you described that needs:

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;
  • After posting I remembered the class Tuple I think it solves, I’ll test and see how it goes !

1

Perhaps the most viable way to create a list is to Dictionary<string, string>, creating 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}");

1

Dude I really like the class Tuple, take the 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: https://docs.microsoft.com/pt-br/dotnet/csharp/tuples

Nuget: https://www.nuget.org/packages/System.ValueTuple/

Remembering that the version of C# has to be 7.0 up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.