Docs Menu
Docs Home
/
MongoDB C# Analyzer

Rules and Message Content

On this page

  • Overview
  • Rule Descriptions
  • MABuilders1001
  • MABuilders2001
  • MALinq1001
  • MALinq2001
  • MALinq2002
  • MAPoco1001
  • MAPoco2001

In this guide, you can learn about the MongoDB C# Analyzer rules and attached messages. A rule evaluates different parts of your code and returns a message if a specific condition is met.

For example, the Analyzer might apply a rule if it detects a LINQ expression, which causes your IDE to display a message that contains the equivalent Query API expression. Each rule has a severity level of Info or Warning that impacts where the message might be recorded.

The following sections describe each C# Analyzer rule, including the rule category, severity level, and description of the corresponding message. Each section also includes an example message.

The MABuilders1001 rule evaluates your code for builders expressions and triggers a message that contains the equivalent Query API expression.

Category
MongoDB.Analyzer.Builders
Severity
Info
Notes
None

The following code triggers the MABuilders1001 rule to display a message that shows the equivalent Query API expression:

var filter = Builders<Book>.Filter.Eq(b => b.Genre, "Theory") &
Builders<Book>.Filter.Gte(b => b.Price, 10);

The following image displays the MABuilders1001 rule and information message:

Screenshot of builder expression with MABuilders1001 rule message

Tip

To learn more about builders expressions, see the Analyze Builders Expressions guide.

The MABuilders2001 rule detects unsupported builders expressions and displays a translation error of the unsupported expression.

Category
MongoDB.Analyzer.Builders
Severity
Warning
Notes
To view examples of unsupported builders expressions, see the source code testing files.

The following code triggers the MABuilders2001 rule to display a message that shows the unsupported builders expression:

var filter = Builders<Book>.Filter.Eq(b => b.Price + 2, 1);

The following image displays the MABuilders2001 rule and information message:

Screenshot of builder expression with MABuilders2001 rule message

Tip

To learn more about builders expressions, see the Analyze Builders Expressions guide.

The MALinq1001 rule evaluates your code for LINQ expressions and triggers a message that contains the equivalent Query API expression.

Category
MongoDB.Analyzer.LINQ
Severity
Info
Notes
This rule might apply differently depending on the value of your DefaultLinqVersion configuration setting. To learn more, see the Analyzer Configuration Options.

The following code triggers the MALinq1001 rule to display a message that shows the equivalent Query API expression:

var results = queryableColl
.Where(b => b.Genre == "Theory")
.OrderBy(b => b.Price);

The following image displays the MALinq1001 rule and information message:

Screenshot of LINQ expression with MALinq1001 rule message

Tip

To learn more about LINQ expressions, see the Analyze LINQ Expressions guide.

The MALinq2001 rule detects unsupported LINQ expressions and displays a translation error of the unsupported expression.

Category
MongoDB.Analyzer.LINQ
Severity
Warning
Notes
This rule might apply differently depending on the value of
your DefaultLinqVersion configuration setting. To learn more,

To view examples of unsupported LINQ expressions, see the

The following code triggers the MALinq2001 rule to display a message that shows the unsupported LINQ expression:

var results = queryableColl.Where(b => b.GetHashCode() == 1);

The following image displays the MALinq2001 rule and information message:

Screenshot of LINQ expression with MALinq2001 rule message

Tip

To learn more about LINQ expressions, see the Analyze LINQ Expressions guide.

The MALinq2002 rule detects if an expression is supported only in LINQ3 and displays a message that contains the equivalent Query API expression.

Category
MongoDB.Analyzer.LINQ
Severity
Warning
Notes
This rule might apply differently depending on the value of
your DefaultLinqVersion configuration setting. To learn more,

To view examples of supported LINQ3 expressions, see the

The following code triggers the MALinq2002 rule to display a message that shows the supported LINQ3 expression:

var results = queryableColl
.Where(b => b.Price + 1 == 21)
.Where(b => "Re-release: " + b.Title == "Re-release: Siddhartha");

The following image displays the MALinq2002 rule and information message:

Screenshot of LINQ expression with MALinq2002 rule message

Tip

To learn more about LINQ expressions, see the Analyze LINQ Expressions guide.

The MAPoco1001 rule evaluates your code for POCO declarations and triggers a message that contains the JSON representation of a sample instance. This rule evaluates some BSON serialization annotations such as BsonIgnore and BsonElement.

Category
MongoDB.Analyzer.Poco
Severity
Info
Notes
This rule might apply differently depending on the value of your PocoAnalysisVerbosity configuration setting. To learn more, see the Analyzer Configuration Options.

The following code triggers the MAPoco1001 rule to display a message that shows the JSON representation of a sample POCO:

public class Person
{
[BsonId]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
[BsonIgnore]
public double Income { get; set; }
}

The following image displays the MAPoco1001 rule and information message:

Screenshot of builder expression with MAPoco1001 rule message

Tip

To learn more about POCO analysis, see the Analyze POCOs guide.

To learn more about POCO annotations, see the POCOs guide in the .NET/C# driver documentation.

The MAPoco2001 rule evaluates your code for unsupported serialization settings specified on a POCO declaration.

Category
MongoDB.Analyzer.Poco
Severity
Warning
Notes
This rule might apply differently depending on the value of
your PocoAnalysisVerbosity configuration setting. To learn more,

To view examples of unsupported POCO declarations, see the

The following code triggers the MAPoco2001 rule to display a message that explains that the code includes an invalid serialization setting on the Date field:

public class Appointment
{
[BsonDateTimeOptions(DateOnly = true, Kind = DateTimeKind.Local)]
public decimal Date { get; set; }
public string Doctor { get; set; }
}

The following image displays the MAPoco2001 rule and information message:

Screenshot of builder expression with MAPoco2001 rule message

Tip

To learn more about POCO analysis, see the Analyze POCOs guide.

To learn more about POCO annotations, see the POCOs guide in the .NET/C# driver documentation.

Back

POCOs