|
| 1 | +.. _visual-studio-extension-builders: |
| 2 | + |
| 3 | +================= |
| 4 | +Analyze Your Code |
| 5 | +================= |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Overview |
| 16 | +-------- |
| 17 | + |
| 18 | +Learn how to use the {+product+} to analyze your {+driver-long+} code. |
| 19 | +The {+product+} can analyze **builder** and **Language Integrated Query (LINQ)** expressions. |
| 20 | + |
| 21 | +A builder is a class provided by the {+driver-short+} to help you construct |
| 22 | +common operations like queries and updates. |
| 23 | + |
| 24 | +LINQ is a query syntax included in the C# language. The {+driver-short+} |
| 25 | +can translate a subset of LINQ expressions into MongoDB aggregation pipelines. |
| 26 | + |
| 27 | +To learn more about builders, see |
| 28 | +`Builders <{+driver-docs+}reference/driver/definitions/>`__ in the |
| 29 | +{+driver-short+} documentation. |
| 30 | + |
| 31 | +To learn more about LINQ, see the following resources: |
| 32 | + |
| 33 | +- `LINQ <{+driver-docs+}reference/driver/crud/linq/>`__ in the {+driver-short+} |
| 34 | + documentation |
| 35 | +- `LINQ <https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/>`__ |
| 36 | + in the Microsoft C# guide |
| 37 | + |
| 38 | +To learn more about aggregation pipelines, see |
| 39 | +:manual:`Aggregation </aggregation/>` in the MongoDB manual. |
| 40 | + |
| 41 | +.. _analyzer-analyze-builders: |
| 42 | + |
| 43 | +Analyze Builders |
| 44 | +---------------- |
| 45 | + |
| 46 | +Use the {+product+} to translate your builder expressions into the {+query-api+}. |
| 47 | +Click the following tabs to see an example of a builder expression |
| 48 | +and its corresponding {+query-api+} translation: |
| 49 | + |
| 50 | +.. tabs:: |
| 51 | + |
| 52 | + .. tab:: Builders |
| 53 | + :tabid: builder |
| 54 | + |
| 55 | + .. code-block:: csharp |
| 56 | + |
| 57 | + var filter = Builders<Movie>.Filter.Eq(m => m.Genre, genre) & |
| 58 | + Builders<Movie>.Filter.Gte(m => m.Score, minScore) & |
| 59 | + Builders<Movie>.Filter.Regex(m => m.Score, titleSearchTerm); |
| 60 | + |
| 61 | + .. tab:: {+query-api+} |
| 62 | + :tabid: query-api-builders |
| 63 | + |
| 64 | + .. code-block:: json |
| 65 | + |
| 66 | + { |
| 67 | + "$and": [ { "Genre": genre }, |
| 68 | + { "Score": { "$gte": minScore } }, |
| 69 | + { "Score": /titleSearchTerm/ } ] |
| 70 | + } |
| 71 | + |
| 72 | +.. include:: /includes/variable-names.rst |
| 73 | + |
| 74 | +Analyze Builders in Visual Studio |
| 75 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 76 | + |
| 77 | +To analyze your builder expressions in Visual Studio, perform the following actions: |
| 78 | + |
| 79 | +1. Install the {+product+} as described in the :doc:`Install </install>` |
| 80 | + guide. |
| 81 | +2. Write a builder expression with the {+driver-short+} |
| 82 | +3. Move your mouse over the :guilabel:`...` annotation beneath the first |
| 83 | + method of your builder expression to display an information message that contains |
| 84 | + the {+query-api+} translation. |
| 85 | + |
| 86 | +Click the following tabs to see a builder expression with and without an |
| 87 | +information message displayed: |
| 88 | + |
| 89 | +.. tabs:: |
| 90 | + |
| 91 | + .. tab:: Without Information Message |
| 92 | + :tabid: no-message |
| 93 | + |
| 94 | + .. figure:: /includes/images/builder.png |
| 95 | + :alt: Screenshot of builder expression in visual studio with ellipsis annotation. |
| 96 | + |
| 97 | + .. tab:: With Information Message |
| 98 | + :tabid: message |
| 99 | + |
| 100 | + .. figure:: /includes/images/builder-popup-photoshop.png |
| 101 | + :alt: Screenshot of builder expression in visual studio with information message displayed. |
| 102 | + |
| 103 | +.. include:: /includes/error-list-window.rst |
| 104 | + |
| 105 | +.. _analyzer-analyze-linq: |
| 106 | + |
| 107 | +Analyze LINQ |
| 108 | +------------ |
| 109 | + |
| 110 | +Use the {+product+} to learn the following |
| 111 | +about your LINQ expressions: |
| 112 | + |
| 113 | +- How your LINQ expressions translate into the {+query-api+} |
| 114 | +- If any of your LINQ expressions are not supported |
| 115 | + |
| 116 | +Click the following tabs to see an example of a LINQ expression |
| 117 | +and its corresponding {+query-api+} translation: |
| 118 | + |
| 119 | +.. tabs:: |
| 120 | + |
| 121 | + .. tab:: LINQ |
| 122 | + :tabid: linq |
| 123 | + |
| 124 | + .. code-block:: csharp |
| 125 | + |
| 126 | + var movies = await moviesCollection.Where(m => |
| 127 | + m.Genre == genre && |
| 128 | + m.Score >= minScore && |
| 129 | + m.Title.Contains(titleSearchTerm)). |
| 130 | + OrderBy(m => m.Score). |
| 131 | + ToListAsync(); |
| 132 | + |
| 133 | + |
| 134 | + .. tab:: {+query-api+} |
| 135 | + :tabid: query-api-linq |
| 136 | + |
| 137 | + .. code-block:: json |
| 138 | + |
| 139 | + [{ "$match" : { |
| 140 | + "Genre" : genre, |
| 141 | + "Score" : { "$gte" : minScore }, |
| 142 | + "Title" : /titleSearchTerm/s } |
| 143 | + }, |
| 144 | + { "$sort" : { "Score" : 1 } }] |
| 145 | + |
| 146 | +.. include:: /includes/variable-names.rst |
| 147 | + |
| 148 | +Analyze LINQ in Visual Studio |
| 149 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +To analyze your LINQ expressions in Visual Studio, perform the following actions: |
| 152 | + |
| 153 | +1. Install the {+product+} as described in the :doc:`Install </install>` |
| 154 | + guide. |
| 155 | +2. Write a LINQ expression with the {+driver-short+}. |
| 156 | +3. Move your mouse over the :guilabel:`...` annotation beneath the first |
| 157 | + method of your LINQ expression to display an information message that contains |
| 158 | + the {+query-api+} translation. |
| 159 | + |
| 160 | +Click the following tabs to see a LINQ expression with and without an information message |
| 161 | +displayed: |
| 162 | + |
| 163 | +.. tabs:: |
| 164 | + |
| 165 | + .. tab:: Without Information Message |
| 166 | + :tabid: without-message |
| 167 | + |
| 168 | + .. figure:: /includes/images/linq.png |
| 169 | + :alt: Screenshot of builder expression in visual studio with ellipsis annotation. |
| 170 | + |
| 171 | + .. tab:: With Information Message |
| 172 | + :tabid: with-message |
| 173 | + |
| 174 | + .. figure:: /includes/images/linq-popup.png |
| 175 | + :alt: Screenshot of builder expression in visual studio with information message displayed. |
| 176 | + |
| 177 | + |
| 178 | +If your LINQ expression is not supported, the {+product+} outputs a |
| 179 | +``NotSupportedLinqExpression`` warning. |
| 180 | + |
| 181 | +Click the following tabs to see a code snippet containing an unsupported LINQ expression |
| 182 | +and the corresponding warning message displayed by the {+product+}: |
| 183 | + |
| 184 | +.. tabs:: |
| 185 | + |
| 186 | + .. tab:: Code Snippet |
| 187 | + :tabid: code-snippet |
| 188 | + |
| 189 | + The following code snippet contains the unsupported ``GetHashCode`` LINQ expression: |
| 190 | + |
| 191 | + .. code-block:: csharp |
| 192 | + |
| 193 | + _ = moviesCollection.Where(m => m.GetHashCode() == 1234); |
| 194 | + |
| 195 | + The following screenshot shows the annotation displayed by the {+product+} |
| 196 | + underneath the preceding code snippet in Visual Studio: |
| 197 | + |
| 198 | + .. figure:: /includes/images/linq-unsupported.png |
| 199 | + :alt: Screenshot of annotation beneath unsupported LINQ expression |
| 200 | + |
| 201 | + .. tab:: Warning |
| 202 | + :tabid: warning |
| 203 | + |
| 204 | + The following is the warning generated by the {+product+}: |
| 205 | + |
| 206 | + .. code-block:: text |
| 207 | + :copyable: false |
| 208 | + |
| 209 | + NotSupportedLinqExpression C# {document}.GetHashCode() is not supported. |
| 210 | + |
| 211 | + The following screenshot shows the warning displayed in Visual Studio: |
| 212 | + |
| 213 | + .. figure:: /includes/images/linq-unsupported-popup.png |
| 214 | + :alt: Screenshot of warning displayed in Visual Studio from unsupported LINQ. |
| 215 | + |
| 216 | +.. include:: /includes/error-list-window.rst |
| 217 | + |
| 218 | +To view more examples of unsupported LINQ expressions, see the |
| 219 | +`{+product+} Github repository <{+product-source-repo+}/tests/MongoDB.Analyzer.Tests.Common.TestCases/Linq/NotSupportedLinqExpressionsBasicTestCases.cs>`__. |
| 220 | + |
| 221 | +Analyze LINQ3 |
| 222 | +~~~~~~~~~~~~~ |
| 223 | + |
| 224 | +To analyze a LINQ3 expression, you must configure the {+product+} to use the LINQ3 |
| 225 | +provider. To learn how to configure your LINQ provider, see the |
| 226 | +:doc:`configuration </configuration>` guide. |
| 227 | + |
| 228 | +.. important:: Expressions Supported Only by LINQ3 |
| 229 | + |
| 230 | + If your {+driver-short+} version supports LINQ3 but you configure your {+product+} to use |
| 231 | + the default LINQ provider (LINQ2), the {+product+} informs you if your LINQ expression |
| 232 | + is supported by LINQ3 but not LINQ2. |
| 233 | + |
| 234 | + Click the tabs to see a LINQ expression supported by LINQ3 but not LINQ2 and the |
| 235 | + corresponding warning output by the {+product+}: |
| 236 | + |
| 237 | + .. tabs:: |
| 238 | + |
| 239 | + .. tab:: Code Snippet |
| 240 | + :tabid: code-snippet-linq3 |
| 241 | + |
| 242 | + .. code-block:: csharp |
| 243 | + |
| 244 | + _ = moviesCollection.Where(m => m.Producer.Substring(0, 6) == "Steven") |
| 245 | + |
| 246 | + .. tab:: Warning |
| 247 | + :tabid: warning-linq3 |
| 248 | + |
| 249 | + .. code-block:: text |
| 250 | + :copyable: false |
| 251 | + |
| 252 | + NotSupportedLinq2Expression Supported in LINQ3 only: db.coll.Aggregate([{ "$match" : { "$expr" : { "$eq" : [{ "$substrCP" : ["$Producer", 0, 6] }, "Steven"] } } }]) |
| 253 | + |
| 254 | +To learn more about LINQ3, see `LINQ3 <{+driver-docs+}reference/driver/crud/linq3/>`__ |
| 255 | +in the {+driver-short+} documentation. |
| 256 | + |
| 257 | +To view examples of expressions the {+driver-short+} only supports with the LINQ3 provider, see the |
| 258 | +`{+product+} Github repository <{+product-source-repo+}/tests/MongoDB.Analyzer.Tests.Common.TestCases/Linq/NotSupportedLinq2TestCases.cs>`__. |
| 259 | + |
| 260 | +Use the {+product+} From the Command Line |
| 261 | +---------------------------------------------- |
| 262 | + |
| 263 | +To run the {+product+} from the command line and save your results to a |
| 264 | +:github:`SARIF </microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md>` |
| 265 | +format file, perform the following actions: |
| 266 | + |
| 267 | +- Install the {+product+} as described in the :doc:`Install </install>` guide. |
| 268 | +- Execute the following command: |
| 269 | + |
| 270 | +.. code-block:: shell |
| 271 | + |
| 272 | + dotnet build -property:ErrorLog=<Path to save your {+product+} report> |
| 273 | + |
| 274 | +To learn more about ``dotnet build``, see |
| 275 | +`.NET Fundamentals <https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build>`__ |
| 276 | +from Microsoft. |
| 277 | + |
| 278 | +To learn more about the ``ErrorLog`` setting, see |
| 279 | +`Error and Warning Options <https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#errorlog>`__ |
| 280 | +from Microsoft. |
0 commit comments