I
I
iamserge2019-07-09 01:15:48
SQL
iamserge, 2019-07-09 01:15:48

Data type similar to table in C#?

In general, it would seem that the task is super simple, but I can’t figure it out in C # ... In general, I have data collected a little bit, but I can’t enter them into the table: firstly, it doesn’t exist at all, secondly, the data will not be needed after one request. Those. I only use them once... It looks like a schedule:
id int, title string, time_action DateTime;
1, Ivan Sergeevich, 05/20/2019 18:45
2, Petr Nikolaevich, 05/20/2019 16:45
3, Svetlana Ivanovna, 05/20/2019 12:30
4, Innokenty Petrov, 05/20/2019 21:15
5, Konstantin Alekseevich, 05/20 .2019 03:25
6, Nikita Valerievich, 05/20/2019 11:08
This is how the table turned out, that is, earlier I would have done "SELECT * FROM table_name ORDER BY time_action" and that's it. But here I have to do exactly the same thing, but using arrays or other data types in C # without any databases ... How to be? So far I have found a struct and I can apparently do all sorts of different things with it, but just such sorts so that I can’t be sure .. In general, apparently, the task is simple, but my level in C # is too low :(
Tell me, who did this, how be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lil_toady, 2019-07-09
@iamserge

That's right, a struct or a class will suit you to describe one record (string), the difference between them will be that the first one is a value type, it cannot be null. And then any collection, even a simple array.
Let's say you've described an entity like this:

class Item
{
    public int Id { get; set; }
    public string Title { get; set; }
    public DateTime Time { get; set; }
}

for simplicity, we use List - the simplest dynamic list, you can add, delete, etc.:
var timeTable = new List<Item>
{
  new Item { Id = 1, Title = "Иван Сергеевич", Time = DateTime.Parse("20.05.2019 18:45") },
  new Item { Id = 2, Title = "Петр Николаевич", Time = DateTime.Parse("20.05.2019 16:45") },
};

And now, through Linq (using System.Linq;), you can work with this data, it works on any collection or array that implements the IEnumerable interface, and provides various methods like Where , Min , Max , OrderBy , etc.
PS Such a record, more similar to SQL, is also possible, but I think the approach with methods will still be more convenient
var ordered = from item in timeTable orderby item.Time ascending select item;

#
#, 2019-07-09
@mindtester

if sorting has already occurred to you, then all the same, an array of structures or classes (as analogues of database rows) .. after that, you can discover C# lists and dictionaries
instead of arrays. The next level of the game is Entity Framework , a very simplified work with the database. just for your case, the in-memory database
option may be of interest here, it all depends on the task. the options I proposed are rather focused on the enterprise , and if you need a very lightweight solution - then all the same, just arrays of classes or structures

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question