Docs Menu
Docs Home
/ / /
EF Core Provider
/

Query Data

On this page

  • Overview
  • Find Entities
  • Find a Single Entity
  • Find Multiple Entities
  • Sort Entities
  • Additional Information

Entity Framework Core allows you to work with data in your application without explicitly running database commands. To query your data, use the Language-Integrated Query (LINQ) syntax. LINQ allows you to write strongly typed queries using C#-specific keywords and operators. When you run the application, the EF Core Provider automatically translates the LINQ queries and runs them on the database using the MongoDB Query API.

In this guide you can see examples of common query operations on an application configured to use the EF Core Provider.

Tip

To learn how to configure an application to use the EF Core Provider, see Configure the EF Core Provider.

Find a single entity by using the FirstOrDefault() method, or find multiple entities by using the Where() method.

The FirstOrDefault() method returns the first entity it finds in your collection that matches the search criteria, and returns null if no matching entities are found.

The following code uses the FirstOrDefault() method to find a planet with the name field of "Mercury" from a DBSet called Planets and prints the planet name to the console:

var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
Console.WriteLine(planet.name);

You can use the Where() method to retrieve multiple entities from your collections. Where() returns all entities that match the search criteria.

The following code uses the Where() method to find all planets that have the hasRings field set to true and prints the planet names to the console.

var planets = db.Planets.Where(p => p.hasRings);
foreach (var p in planets)
{
Console.WriteLine(p.name);
}

Use the OrderBy() method to specify an order in which to return entities from a query. OrderBy() sorts the elements in ascending order based on a specified sort criteria.

The following code uses the OrderBy() method to find all planets and sort them by the value of the orderFromSun field in ascending order. It then prints the results to the console.

var planetList = db.Planets.OrderBy(p => p.orderFromSun);
foreach (var p in planetList)
{
Console.WriteLine(p.name);
}
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Tip

Sort in Descending Order

You can sort the results of a query in descending order by using the OrderByDescending() method.

You can perform a secondary sort on your query by using the ThenBy() method. The ThenBy() method sorts the results of the OrderBy() method in ascending order based on a specified sort criteria. The ThenBy() method should be chained to the OrderBy() method.

Tip

Secondary Sort in Descending Order

You can perform a secondary sort in descending order by using the ThenByDescending() method.

The following code uses the OrderBy() and ThenBy() methods to find all planets and sort them by the hasRings() field, with a secondary sort on the name field.

var planetList = db.Planets.OrderBy(o => o.hasRings).ThenBy(o => o.name);
foreach (var p in planetList)
{
Console.WriteLine("Has rings: " + p.hasRings + ", Name: " + p.name);
}
Has rings: False, Name: Earth
Has rings: False, Name: Mars
Has rings: False, Name: Mercury
Has rings: False, Name: Venus
Has rings: True, Name: Jupiter
Has rings: True, Name: Neptune
Has rings: True, Name: Saturn
Has rings: True, Name: Uranus

Tip

When sorting on fields with a boolean value, entities with a field value of false show before those with a value of true.

To learn more about the methods discussed in this guide, see the following .NET API documentation links:

  • FirstOrDefault()

  • Where()

  • OrderBy()

  • OrderByDescending()

  • ThenBy()

  • ThenByDescending()

Back

Configuration