Query Gallery

Menu
Menu

Sample Database

The Query Galery is based on the sample database of:

Films Table Design

The assumption is that logic is needed for Controller to return either a single Film model or a List if more than one record results from the query.

It is also assumed a DbSet() of Films has been set up.

Extract One Record by Primary Key

With SQL the query would be:

SELECT * FROM Films WHERE FilmID = 17;

The View would be set up to receive a single entity that matched the model. Assume an id value is sent as a routing parameter.

Use the Find() method of DbSet :

DbSet()

LINQ Query Syntax:

LINQ Query Syntax

LINQ Method Syntax:

LINQ Method Syntax

Extract Multiple Records (no filter)

The View for these examples would be set up to receive a List of entities.

With SQL the query would be:

SELECT * FROM Films;

Use the ToList() method of DbSet :

DbSet()

LINQ Query Syntax:

LINQ Query Syntax

Extract Multiple Records (with WHERE filter)

The View for these examples would be set up to receive a List of entities.

With SQL the query would be:

SELECT * FROM Films WHERE FilmCertificate = 'PG';

LINQ Query Syntax:

LINQ Query Syntax

LINQ Method Syntax:

LINQ Method Syntax

Extract Multiple Records (with Multiple WHERE filters)

The View for these examples would be set up to receive a List of entities.

With SQL the query would be:

SELECT * FROM Films WHERE FilmCertificate = 'PG' AND FilmPrice > 4;

LINQ Query Syntax:

LINQ Query Syntax

LINQ Method Syntax:

LINQ Method Syntax

Extract Mutiple Record With a Contains Query

With SQL the query would be:

SELECT * FROM Films WHERE FilmTitle LIKE '%stars%';

The View for these examples would be set up to receive a List of entities.

LINQ Query Syntax:

LINQ Query Syntax

LINQ Method Syntax:

LINQ Method Syntax