S
S
Stadinov Denis2014-06-11 11:24:46
Arrays
Stadinov Denis, 2014-06-11 11:24:46

How do you replace multidimensional associative arrays in c#?

Tell me how such things are implemented in c#?
In php this is called multidimensional associative arrays, below is an example:

massiv[i] = array{ 
name => "", 
size => "", 
position => "", 
text  => ""
}

In my project, data about an object is pulled out from the database, I want to write them all down, number them, and so that everything would be on the shelves.
Thank you!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
AlexP11223, 2014-06-11
@AlexP11223

In my project, data about an object is pulled out from the database

Well, create a class and an object (you can also look at the ORM of the Entity Framework type), and what does the array have to do with it?
In general, for associative arrays, you can use the Dictionary msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.1...

B
Boxxy, 2014-06-11
@Boxxy

public class SomeClassName {
    public string Name {get;set;}
    public int Size {get;set;}
    public int Position {get;set;}
    public string Text {get;set;}
}

List<SomeClassName> yourArray = new List<SomeClassName>();

yourArray.Add(new SomeClassName {
    Name = "abc",
    Size = 123,
    Position = 123,
    Text = "abc"
});

In general, as @AlexP11223 said , look towards the Entity Framework.

A
Artem Voronov, 2014-06-11
@newross

@AlexP11223 wrote correctly. If you do not want to use ORM, but only ADO.NET, then the fields of the record will need to be assigned to the properties of the object. The objects themselves can be stored in a list.
The simplest example:

public class Sample
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public List<Sample> GetSamples()
{
    var result = new List<Sample>();
    using (var connection = new SqlConnection("connection_string"))
    {
        var command = connection.CreateCommand();
        command.CommandText = "SELECT id, name FROM .....";
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                result.Add(new Sample
                {
                    Id = reader.GetInt32(0),
                    Name = reader.GetString(1)
                });
            }
            reader.Close();
        }
        connection.Close();
    }
    return result;
}

G
gleb_kudr, 2014-06-13
@gleb_kudr

If you choose arbitrary elements from the list and not just store them, then do not forget to get acquainted with collections like HashSet and Dictionary in addition to List. There are tasks when this simple replacement leads to a thousandfold increase in productivity.

A
Anton Martsen, 2014-06-12
@martsen

I join the above. You can also look in the direction of Dapper, if you want everything to "map" itself.
PS
It if decided to go by pure ADO.NET.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question