|
| 1 | +.. _entity-framework-query-data: |
| 2 | + |
| 3 | +========== |
| 4 | +Query Data |
| 5 | +========== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: EF, EF Core, code example, read |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +{+framework-core+} allows you to work with data in your application without |
| 24 | +explicitly running database commands. To query your data, use the |
| 25 | +Langage-Integrated Query (LINQ) syntax. LINQ allows you to write strongly typed |
| 26 | +queries using C#-specific keywords and operators. When you run the application, |
| 27 | +the {+provider-long+} automatically translates the LINQ queries and runs them on the |
| 28 | +database using the MongoDB Query API. |
| 29 | + |
| 30 | +In this guide you can see examples of common query operations on |
| 31 | +an application configured to use the {+provider-long+}. |
| 32 | + |
| 33 | +.. tip:: |
| 34 | + |
| 35 | + To learn how to configure an application to use the {+provider-short+}, see |
| 36 | + :ref:`entity-framework-configure`. |
| 37 | + |
| 38 | +Find Entities |
| 39 | +------------- |
| 40 | + |
| 41 | +Find a single entity by using the ``FirstOrDefault()`` method, or find |
| 42 | +multiple entities by using the ``Where()`` method. |
| 43 | + |
| 44 | +Find a Single Entity |
| 45 | +~~~~~~~~~~~~~~~~~~~~ |
| 46 | + |
| 47 | +The ``FirstOrDefault()`` method returns the first entity it finds in your collection that |
| 48 | +matches the search criteria, and returns ``null`` if no matching entities |
| 49 | +are found. |
| 50 | + |
| 51 | +The following code uses the ``FirstOrDefault()`` method to find a planet with |
| 52 | +the ``name`` field of "Mercury" from a ``DBSet`` called ``Planets`` and prints |
| 53 | +the planet name to the console: |
| 54 | + |
| 55 | +.. literalinclude:: /includes/code-examples/quick-reference.cs |
| 56 | + :start-after: // start-find-one |
| 57 | + :end-before: // end-find-one |
| 58 | + :language: csharp |
| 59 | + :dedent: |
| 60 | + |
| 61 | +To learn more about the ``FirstOrDefault()`` method, see the `.NET API documentation |
| 62 | +<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.firstordefault?view=net-8.0>`__. |
| 63 | + |
| 64 | +Find Multiple Entities |
| 65 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 66 | + |
| 67 | +You can use the ``Where()`` method to retrieve multiple entities from your |
| 68 | +collections. ``Where()`` returns all entities that match the search |
| 69 | +criteria. |
| 70 | + |
| 71 | +The following code uses the ``Where()`` method to find all planets that have the |
| 72 | +``hasRings`` field set to ``true`` and prints the planet names to the console. |
| 73 | + |
| 74 | +.. literalinclude:: /includes/code-examples/quick-reference.cs |
| 75 | + :start-after: // start-find-many |
| 76 | + :end-before: // end-find-many |
| 77 | + :language: csharp |
| 78 | + :dedent: |
| 79 | + |
| 80 | +To learn more about the ``Where()`` method, see the `.NET API documentation |
| 81 | +<https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.query.queryablemethods.where>`__. |
| 82 | + |
| 83 | +Sort Entities |
| 84 | +------------- |
| 85 | + |
| 86 | +Use the ``OrderBy()`` method to specify an order in which to return entities |
| 87 | +from a query. ``OrderBy()`` sorts the elements in ascending order based on a |
| 88 | +specified sort criteria. |
| 89 | + |
| 90 | +The following code uses the ``OrderBy()`` method to find all planets and sort |
| 91 | +them by the value of the ``orderFromSun`` field in ascending order. It then |
| 92 | +prints the results to the console. |
| 93 | + |
| 94 | +.. io-code-block:: |
| 95 | + :copyable: true |
| 96 | + |
| 97 | + .. input:: |
| 98 | + :language: csharp |
| 99 | + |
| 100 | + var planetList = db.Planets.OrderBy(p => p.orderFromSun); |
| 101 | + |
| 102 | + foreach (var p in planetList) |
| 103 | + { |
| 104 | + Console.WriteLine(p.name); |
| 105 | + } |
| 106 | + |
| 107 | + .. output:: |
| 108 | + :language: json |
| 109 | + :visible: false |
| 110 | + |
| 111 | + Mercury |
| 112 | + Venus |
| 113 | + Earth |
| 114 | + Mars |
| 115 | + Jupiter |
| 116 | + Saturn |
| 117 | + Uranus |
| 118 | + Neptune |
| 119 | + |
| 120 | +To learn more about the ``OrderBy()`` method, see the `.NET API documentation |
| 121 | +<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderby>`__. |
| 122 | + |
| 123 | +.. tip:: Sort in Descending Order |
| 124 | + |
| 125 | + You can sort the results of a query in descending order by using the |
| 126 | + ``OrderByDescending()`` method. |
| 127 | + |
| 128 | + To learn more about the ``OrderByDescending()`` method, see the `.NET API documentation |
| 129 | + <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderbydescending>`__. |
| 130 | + |
| 131 | +You can perform a secondary sort on your query by using the ``ThenBy()`` method. The |
| 132 | +``ThenBy()`` method sorts the results of the ``OrderBy()`` method in ascending |
| 133 | +order based on a specified sort criteria. The ``ThenBy()`` method should be |
| 134 | +chained to the ``OrderBy()`` method. |
| 135 | + |
| 136 | +.. tip:: Secondary Sort in Descending Order |
| 137 | + |
| 138 | + You can perform a secondary sort in descending order by using the |
| 139 | + ``ThenByDescending()`` method. |
| 140 | + |
| 141 | + To learn more about the ``ThenByDescending()`` method, see the `.NET API documentation |
| 142 | + <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenbydescending>`__. |
| 143 | + |
| 144 | +The following code uses the ``OrderBy()`` and ``ThenBy()`` methods to find all |
| 145 | +planets and sort them by the ``hasRings()`` field, with a secondary sort |
| 146 | +on the ``name`` field. |
| 147 | + |
| 148 | +.. io-code-block:: |
| 149 | + :copyable: true |
| 150 | + |
| 151 | + .. input:: |
| 152 | + :language: csharp |
| 153 | + |
| 154 | + var planetList = db.Planets.OrderBy(o => o.hasRings).ThenBy(o => o.name); |
| 155 | + |
| 156 | + foreach (var p in planetList) |
| 157 | + { |
| 158 | + Console.WriteLine("Has rings: " + p.hasRings + ", Name: " + p.name); |
| 159 | + } |
| 160 | + |
| 161 | + .. output:: |
| 162 | + :language: json |
| 163 | + :visible: false |
| 164 | + |
| 165 | + Has rings: False, Name: Earth |
| 166 | + Has rings: False, Name: Mars |
| 167 | + Has rings: False, Name: Mercury |
| 168 | + Has rings: False, Name: Venus |
| 169 | + Has rings: True, Name: Jupiter |
| 170 | + Has rings: True, Name: Neptune |
| 171 | + Has rings: True, Name: Saturn |
| 172 | + Has rings: True, Name: Uranus |
| 173 | + |
| 174 | +.. tip:: |
| 175 | + |
| 176 | + When sorting on fields with a boolean value, entities with a field value of |
| 177 | + ``false`` show before those with a value of ``true``. |
| 178 | + |
| 179 | +To learn more about the ``ThenBy()`` method, see the `.NET API documentation |
| 180 | +<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenby>`__. |
0 commit comments