C
C#2mo ago
Faker

✅ Trying to store an entire table from a database into a hash table

C#
// load each table in a hash map and perform operations

using MainProject.Models;
using MainProject.Context;
using Microsoft.EntityFrameworkCore;

var instructorTable = new HashMap<int, Instructor>();
var studentTable = new HashMap<int, Student>();
var lessonTable = new HashMap<int, Lesson>();
var carTable = new HashMap<int, Car>();

using (var context = new DrivingLessonBookingSystemContext())
{
// Loading instructor table in HashTable
var instructors = context.Instructors.Include(l => l.Lessons);
foreach (var instructor in instructors)
{
instructorTable.Insert(instructor.InstructorId,instructor);
}

// Loading student table in HashTable
var students = context.Students.Include(l => l.Lessons);
foreach (var student in students)
{
studentTable.Insert(student.StudentId,student);
}

// Loading car table in HashTable
var cars = context.Cars.Include(l => l.Lessons);
foreach (var car in cars)
{
carTable.Insert(car.CarId, car);
}

// Loading car table in HashTable
var lessons = context.Lessons
.Include(l => l.Instructor)
.Include(l => l.Car)
.Include(l => l.Student);

foreach (var lesson in lessons)
{
lessonTable.Insert(lesson.LessonId, lesson);
}

}
C#
// load each table in a hash map and perform operations

using MainProject.Models;
using MainProject.Context;
using Microsoft.EntityFrameworkCore;

var instructorTable = new HashMap<int, Instructor>();
var studentTable = new HashMap<int, Student>();
var lessonTable = new HashMap<int, Lesson>();
var carTable = new HashMap<int, Car>();

using (var context = new DrivingLessonBookingSystemContext())
{
// Loading instructor table in HashTable
var instructors = context.Instructors.Include(l => l.Lessons);
foreach (var instructor in instructors)
{
instructorTable.Insert(instructor.InstructorId,instructor);
}

// Loading student table in HashTable
var students = context.Students.Include(l => l.Lessons);
foreach (var student in students)
{
studentTable.Insert(student.StudentId,student);
}

// Loading car table in HashTable
var cars = context.Cars.Include(l => l.Lessons);
foreach (var car in cars)
{
carTable.Insert(car.CarId, car);
}

// Loading car table in HashTable
var lessons = context.Lessons
.Include(l => l.Instructor)
.Include(l => l.Car)
.Include(l => l.Student);

foreach (var lesson in lessons)
{
lessonTable.Insert(lesson.LessonId, lesson);
}

}
Hello guys, consider the above code. What the code does is, it takes each table from the driving lesson database and insert them into a Hash Table. My question is: Was there a better way to store each table into a Hash Table?
8 Replies
Salman
Salman2mo ago
Why do you need to store them in the HashMap in the first place? Also HashMap isn't used much nowadays, you should use Dictionary<K,V>
Jimmacle
Jimmacle2mo ago
if you're storing all your data in memory why even use a database? i'm also not sure where you got HashMap from, that's not in the BCL afaik because the C# equivalent is a Dictionary<TKey, TValue>
Faker
FakerOP2mo ago
No ,.HashMap is something I created, Im not using the in built one, I needed to construct one from scratch for this project :c The thing is I was told to use a "disconnected" architecture to make changes into the database Like I load the objects, modify or add if needed, then connect back to db to upload them Don't know why I should use a disconnected architrcture though, may be tk avoid 2 users modifying same data ?
Salman
Salman2mo ago
Doesn't EF does the same ?
Angius
Angius2mo ago
It does, yes Unless you use .ExecuteUpdateAsync()
Faker
FakerOP2mo ago
hmm I'm a bit lost :c, EF core uses a disconnected approach? so the use of the hash map is irrelevant ?
Salman
Salman2mo ago
Yes
Faker
FakerOP2mo ago
Oh okay, didn't know that Thanks !

Did you find this page helpful?