What I've Learned about LINQ and MVC

minute read

2025-1-19 Back to posts

C# hitting a Go gopher with a hammer.

What I’ve Learned about LINQ and MVC

Learning about C# and the various facets of MVC architecture has been exciting.

I am sharing an example of why using LINQ with Lambdas is quickly becoming my favorite feature in C#.

The Situation

Allow me to lay out this hypothetical for you:

You’re setting up a complex front end with specific business requirements.

You have a model representing a table of products in your database.

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Stock { get; set; }
}

With LINQ and Lambdas, you gain an idiomatic abstraction over SQL queries that is both straightforward and powerful.

Business logic with becomes easy to define and maintain—structured in a familiar way to SQL when using LINQ.

In our example below, we can address the business requirements in a declarative fashion, reducing the need for maintenance and limiting the introduction of bugs.

Need a list of products with a stock greater than 5 but ordered by the cheapest price?

Simple with Lambdas and LINQ:

.Where(p => p.Stock > 5) 

.OrderBy(p => p.Price)

.ToList();

Full Controller Class

public class ProductsController : Controller
{
    private readonly AppDbContext _context;

    public ProductsController(AppDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        // Fetch all products
        var products = _context.Products.ToList();
        
        // Filter products with Price < 2
        var productsToRemove = products.Where(p => p.Price < 2)
        							   .ToList();

        // Remove each filtered product from the database
        _context.Products.RemoveRange(productsToRemove);

        // Save changes to persist the removal
        _context.SaveChanges();
        
        // Fetch a specific product by name
        var laptop = _context.Products.FirstOrDefault(p => p.Name == "Laptop");

        // Fetch products with stock greater than 5, ordered by cheapest price
        var inStock = _context.Products
                              .Where(p => p.Stock > 5)
                              .OrderBy(p => p.Price)
                              .ToList();
                              
        // Fetch up to 10 products named "Laptop"
        var laptops = _context.Products
                              .Where(p => p.Name == "Laptop")
                              .Take(10)
                              .ToList();
		
        // Pass the data to the view
        return View(products);
    }
}

Conclusion

Instead of writing verbose SQL queries or manually iterating through collections, LINQ with Lambdas allows us to express business logic concisely.

Written By Nick Stambaugh

Nick Stambaugh

Nick Stambaugh

Full Stack Engineer

Full Stack Enterprise Application Engineer

Recent Posts

Adding Music To My Raylib App

Adding audio to a C application is easy with raylib!

2025-03-13

Read more →

Why I Love Houston As A Michigander

Houston is an amazing city to explore with a wide variety of interesting activities, culture, and food.

2025-02-22

Read more →

What I've Learned about LINQ and MVC

An example of why I'm starting to love C# more than Go.

2025-1-19

Read more →

[PART 1] Creating A SQL-Like Language in C#

Part 1 of creating a SQL-like language in C# for fun.

2025-1-3

Read more →

A Simple Bar-Chart Console App in TypeScript

Creating an ASCII bar chart generator in the console with TypeScript.

2024-12-27

Read more →

A New (Unfinished) React Subscribe Component

A new React component for easy email subscriptions on your website.

2024-12-14

Read more →