Docs Menu
Docs Home
/ / /
Entity Framework
/

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 MongoDB Entity Framework 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 MongoDB Entity Framework Core Provider.

Tip

To learn how to configure an application to use the Entity Framework Core Provider, see Configure Entity Framework Core for MongoDB.

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);
}

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);
}

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()

← Configure Entity Framework Core for MongoDB