Posts

Showing posts from September, 2025

🎓 HackerRank Practice: Student Marks Manager using C# Lists

Working with Lists is one of the most common exercises in coding challenges. Lists in C# are dynamic collections that allow adding, removing, and inserting elements easily. Let’s solve a simple HackerRank-style problem step by step. 📌 Problem Statement: Student Marks Manager You are given student marks. Perform operations using a List<int> . Input 5 10 20 30 40 50 3 Add 60 Remove 20 Insert 25 2 Output 10 25 30 40 50 60 Explanation Start: [10, 20, 30, 40, 50] Add 60 → [10, 20, 30, 40, 50, 60] Remove 20 → [10, 30, 40, 50, 60] Insert 25 at index 2 → [10, 25, 30, 40, 50, 60] ✅ This tests: Index-based access Add / Remove operations Insert at a given index 🛠️ Solution in C# using System; using System.Collections.Generic; public class Program { public static void Main() { // Input: number of students int n = int.Parse(Console.ReadLine()); // Input: student marks string[] input = Console.ReadLin...

📌 Indexes in EF Core

Indexes are one of the most powerful tools for query performance tuning. In Entity Framework Core (EF Core), indexes help: ⚡ Speed up lookups, filtering, and sorting 🔒 Enforce uniqueness 🏢 Support multi-tenant and domain-specific rules This guide walks through everything you need to know — from the basics to advanced scenarios — with real-world code samples and best practices. ✅ What is an Index in EF Core? An index is a database object that allows the database to locate rows faster. Use .HasIndex() → to create indexes via Fluent API Use .IsUnique() → to enforce uniqueness EF Core automatically creates indexes for foreign key columns ✅ Defining Indexes in EF Core Type Description Basic Index Creates a non-unique index on the Name column. Unique Index Ensures no two rows share the same Sku (enforced at the DB level). Composite Index (Multi-Column) Used when queries filter on multiple fields together. Example: WHER...

🔑 Keys in EF Core

✅ 1. Primary Key (PK) Definition: Uniquely identifies each entity. Default Convention: Property named Id or <EntityName>Id . Explicit Definition: Data Annotation: [Key] public int ProductId { get; set; } Fluent API: modelBuilder.Entity<Product>() .HasKey(p => p.ProductId); ✅ 2. Composite Key modelBuilder.Entity<OrderDetail>() .HasKey(od => new { od.OrderId, od.ProductId }); ⚠️ Must be defined using Fluent API (Data Annotations don’t support composite keys). ✅ 3. Alternate Key modelBuilder.Entity<Product>() .HasAlternateKey(p => p.Sku); ✅ Primary Key vs Alternate Key Feature Primary Key Alternate Key Uniquely identifies? ✅ Yes ✅ Yes Required? ✅ Yes ❌ Optional Can be used as FK? ✅ Yes ✅ Yes (with .HasPrincipalKey ) ✅ 4. Foreign Key (FK) modelBuilder.Entity<Employee...