LINQ

Unit Test for EF LINQ queries using Mocked DbSet

Goal: Create a unit test for a EF repository query. As we all know unit tests are infrastructure agnostic and therefore have no kowledge of for example external services, databases, etc. So how can we create a unit test for a repository query without hitting the DB? Mocked DbSet to the rescue:Solution:Create an extension method on collections that can be used to mock the data/collections returned by EF instead of hitting the DB.//A List of allocations for a gas system that we want to return when ......

LINQ Contains Case Insensitive

Goal: Search for content in the database via LINQ to ENTITIES or LINQ to SQL that is case-insensitive (case-agnostic). Example: var con = BusinessContext.Create(); var owners = con.Shipments.Where(s => s.Dispatches.All(x => x.DriverLBCompany.Contains(... ).Select(x => x); This will fetch all dispatches where the DriverLBCompany contains the word “(exempt)”. You do not have to be concerned about the case as SQL Server is case-agnostic when it performs filtering. The sql being generated ......