SonarQube Analysis / sonarqube (pull_request) Successful in 3m9s
Introduce a default regex match timeout across the library to prevent potential ReDoS attacks (SonarQube rule S6444). Implement `[OnDeserialized]` methods to re-establish object invariants and validate state after deserialization, addressing SonarQube rule S5766.
Strata.SqlTools.LinqToSql
LINQ to SQL support for Strata SQL Utilities, providing query breakdown and analysis capabilities for LINQ to SQL queries.
Overview
This library extends Strata.SqlTools to work with LINQ to SQL queries, allowing you to:
- Analyze LINQ query expressions
- Break down LINQ queries into their component parts
- Convert LINQ expressions to QueryBreakdown objects
- Generate SQL representations from LINQ queries
- Visualize LINQ query structure
Features
LINQ Query Analysis
- Extract SELECT, WHERE, JOIN, GROUP BY, and ORDER BY operations from LINQ expressions
- Identify data sources and table references
- Analyze query composition and complexity
QueryBreakdown Integration
- Convert LINQ
IQueryable<T>toQueryBreakdownobjects - Support for common LINQ methods:
Where,Select,OrderBy,GroupBy,Join, etc. - Parameter extraction and analysis
Expression Visitors
- Custom expression visitors for LINQ expression trees
- Support for method call expressions, lambda expressions, and member access
- Handles both query syntax and method syntax
Installation
dotnet add package Strata.SqlTools.LinqToSql
Usage
Basic Query Breakdown
using Strata.SqlTools.Breakdowns.LinqToSql;
using System.Linq;
// Your LINQ to SQL query
var query = from user in context.Users
where user.Age > 21
orderby user.Name
select new { user.Id, user.Name, user.Email };
// Analyze the query
var breakdown = LinqQueryBreakdown.Analyze(query);
// Access breakdown components
Console.WriteLine($"Select: {breakdown.SelectClause}");
Console.WriteLine($"From: {breakdown.FromClause}");
Console.WriteLine($"Where: {breakdown.WhereClause}");
Console.WriteLine($"OrderBy: {breakdown.OrderByClause}");
Expression Analysis
using Strata.SqlTools.Visitors.LinqToSql;
// Analyze a specific expression
Expression<Func<User, bool>> predicate = u => u.Age > 21 && u.Status == "Active";
var visitor = new LinqExpressionVisitor();
visitor.Visit(predicate);
// Get analysis results
var conditions = visitor.GetConditions();
var parameters = visitor.GetParameters();
SQL Generation
// Generate SQL from LINQ query
var breakdown = LinqQueryBreakdown.Analyze(query);
string sql = breakdown.GetSql();
Console.WriteLine(sql);
// Output: SELECT u.Id, u.Name, u.Email FROM Users u WHERE u.Age > 21 ORDER BY u.Name
Architecture
Key Components
- LinqQueryBreakdown: Main class for analyzing LINQ queries and converting them to breakdown format
- LinqExpressionVisitor: Expression visitor for traversing LINQ expression trees
- LinqToSqlConverter: Converts LINQ expressions to SQL Server QueryBreakdown objects
Supported LINQ Methods
Where→ WHERE clauseSelect→ SELECT clauseOrderBy,OrderByDescending,ThenBy,ThenByDescending→ ORDER BY clauseGroupBy→ GROUP BY clauseJoin,GroupJoin→ JOIN clausesFirst,FirstOrDefault,Single,SingleOrDefault→ TOP 1Take,Skip→ TOP n / OFFSET-FETCHDistinct→ DISTINCTCount,Sum,Average,Min,Max→ Aggregate functions
Limitations
- LINQ to SQL translates to SQL Server T-SQL dialect
- Complex expressions may not be fully analyzed
- Some LINQ features may not have direct SQL equivalents
- Requires the query to be
IQueryable<T>(notIEnumerable<T>)
Integration with Markdown
Use with Strata.SqlTools.Markdown to generate visual diagrams:
using Strata.SqlTools.Markdown.LinqToSql;
var breakdown = LinqQueryBreakdown.Analyze(query);
var generator = new QueryBreakdownGenerator();
string mermaidDiagram = generator.GenerateMermaidDiagram(breakdown, "User Query");
See Also
- Strata.SqlTools - Core SQL utilities
- Strata.SqlTools.SqlServer - SQL Server support
- Strata.SqlTools.Markdown - Markdown generation
License
MIT License - Copyright © Strata Decision Technology 2024-2026