Linq
The LINQ system, which allows you to write SQL-like queries, can be used naturally in AventusSharp.
Example: LINQ Query on a User
var users = User.Where(u => u.Name == "John");
However, there are some restrictions on the functions that can be used in LINQ queries. The following functions are supported:
Supported LINQ Functions
String Functions
You can use the following string functions in LINQ queries:
StartsWith
Contains
EndsWith
ToLower
ToUpper
Example:
var result = User.Where(u => u.Name.StartsWith("John"));
DateTime Functions
For DateTime
or Datetime
or Date
types, you can use the following properties:
Year
Month
Day
Hour
Minute
Second
Example:
var result = User.Where(u => u.CreatedAt.Year == 2024);
List Functions
You can use Contains
with lists of simple types like int
, decimal
, double
, float
, string
, bool
, DateTime
, or Date
.
List<int>
List<decimal>
List<double>
List<float>
List<string>
List<bool>
List<DateTime>
List<Date>
Example (for a list of integers):
List<int> ids = new List<int>() { 1, 2 };var result = User.Where(u => ids.Contains(u.Id));
List of Storable
For lists of objects that implement the Storable
interface, you can use Contains
as well.
Example (for a list of tags):
var result = User.Where(u => u.Tags.Contains(new Tag { Id = 1 }));
Debugging SQL Queries
If the generated SQL queries don’t seem correct, you can set the Debug
boolean to true
on your MySQLStorage
object. This will enable query logging, allowing you to see the SQL queries being executed against the database.
This feature can help you troubleshoot issues by providing the exact SQL query, which you can then use when opening an issue. Make sure to include both the source code and the SQL result when reporting the problem.
// storage is MySQLStorageAventus.storage.Debug = true;
Once enabled, all the SQL queries generated by your LINQ operations will be displayed, making it easier to track down potential issues.
Conclusion
LINQ in AventusSharp allows you to query objects using common functions, but it has some limitations. Make sure to use only the supported functions listed above when writing your queries.