Docs Menu

Docs HomeMongoDB Analyzer

Analyze LINQ Expressions

On this page

  • Overview
  • Translate into the MongoDB Query API
  • Analyze LINQ in Visual Studio
  • Simple LINQ Expressions
  • Unsupported LINQ Expressions
  • Analyze LINQ3

LINQ is a query syntax included in the C# language. The .NET/C# driver can translate a subset of LINQ expressions into MongoDB aggregation pipelines.

To learn more about LINQ, see the following resources:

  • LINQ in the .NET/C# driver documentation

  • LINQ in the Microsoft C# guide

To learn more about aggregation pipelines, see Aggregation in the MongoDB manual.

Note

Runtime Differences

Although the MongoDB Query API translations generated by the MongoDB Analyzer have the same query shape as your runtime .NET/C# driver queries, there may be slight differences due to the following factors:

  • Your Serialization Settings

  • Your LINQ Settings

The difference between the translations the MongoDB Analyzer generates and your queries at runtime should not impact your ability to analyze and debug your code.

To learn more about serialization and LINQ settings, see the FAQ page.

Use the MongoDB Analyzer to learn the following about your LINQ expressions:

  • How your LINQ expressions translate into the MongoDB Query API

  • If any of your LINQ expressions are not supported

Click the following tabs to see an example of a LINQ expression and its corresponding MongoDB Query API translation:

Note

Variable Names

The MongoDB Query API translations generated by the MongoDB Analyzer contain variable names from your .NET/C# driver code. The .NET/C# driver replaces these variable names with their corresponding values when your application communicates with MongoDB.

To analyze your LINQ expressions in Visual Studio, perform the following actions:

  1. Install the MongoDB Analyzer as described in the Install guide.

  2. Write a LINQ expression with the .NET/C# driver.

  3. Move your mouse over the ... annotation beneath the first method of your LINQ expression to display an information message that contains the MongoDB Query API translation.

The MongoDB Analyzer analyzes LINQ expressions in the following syntax types:

  • Method syntax

  • Query syntax

Click on the following corresponding tab to see a LINQ expression written in method syntax with or without an information message displayed:

Click on the following corresponding tab to see a LINQ expression written in query syntax with or without an information message displayed:

If your LINQ expression is not supported, the MongoDB Analyzer outputs a NotSupportedLinqExpression warning.

Click the following tabs to see a code snippet containing an unsupported LINQ expression and the corresponding warning message displayed by the MongoDB Analyzer:

Tip

Error List Panel

If you are using Visual Studio for Windows, you can view the output from the MongoDB Analyzer in the Error List window.

To learn more, see Error List Window from Microsoft.

To view more examples of unsupported LINQ expressions, see the MongoDB Analyzer Github repository.

To analyze a LINQ3 expression, you must configure the MongoDB Analyzer to use the LINQ3 provider. To learn how to configure your LINQ provider, see the configuration guide.

Important

Expressions Supported Only by LINQ3

If your .NET/C# driver version supports LINQ3 but you configure your MongoDB Analyzer to use the default LINQ provider (LINQ2), the MongoDB Analyzer informs you if your LINQ expression is supported by LINQ3 but not LINQ2.

Click the tabs to see a LINQ expression supported by LINQ3 but not LINQ2 and the corresponding warning output by the MongoDB Analyzer:

To view examples of expressions the .NET/C# driver only supports with the LINQ3 provider, see the MongoDB Analyzer Github repository.

← Analyze Builders Expressions