NetDataQuery is an open-source library for .NET that allows dynamic queries to be defined through method names, simplifying data access without writing LINQ expressions manually.
Install the library from NuGet:
Install-Package NetDataQuery
And in your Program.cs
, register the repositories:
builder.Services.AddNetDataRepositories<YourDbContext>();
Define an interface that inherits from INetDataQuery<T>
and declare your query methods using GetBy...
, FindBy...
, ReadBy...
, or QueryBy...
.
The library parses the method name and automatically generates the corresponding LINQ expression.
Assuming the class Line
:
public class Line
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public bool Active { get; set; }
public int BrandId { get; set; }
public Brand? Brand { get; set; }
}
public class Brand
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? IconColor { get; set; }
public bool Active { get; set; }
}
public interface ILineRepository : INetDataQuery<Line>
{
Task<List<Line>> GetByActiveTrue();
Task<List<Line>> GetByNameEquals(string name);
Task<List<Line>> GetByNameAndActiveFalse(string name);
Task<List<Line>> GetByBrandName(string name);
Task<List<Line>> GetByBrandIconColorAndActiveTrueIncludeBrand(string color);
Task<List<Line>> GetAllIncludeBrand();
Task<Line> GetByIdEquals(int id); // Example of Task<Class>
}
var activeLines = await _lineRepository.GetByActiveTrue();
var byName = await _lineRepository.GetByNameEquals("Special");
var byBrand = await _lineRepository.GetByBrandName("Toyota");
var withInclude = await _lineRepository.GetByBrandIconColorAndActiveTrueIncludeBrand("red");
GetBy
,FindBy
,ReadBy
,QueryBy
GetAll
,FindAll
, etc.
Suffix | Example | Generated LINQ Expression |
---|---|---|
Equals (default) | GetByName(string) |
x => x.Name == value |
True | GetByActiveTrue() |
x => x.Active == true |
False | GetByActiveFalse() |
x => x.Active == false |
GreaterThan | GetByIdGreaterThan(int id) |
x => x.Id > id |
LessThan | GetByIdLessThan(int id) |
x => x.Id < id |
You can combine conditions using And
and Or
:
GetByNameAndActiveTrue
GetByIdGreaterThanOrActiveFalse
You can query nested properties of related entities:
GetByBrandName(string name)
GetByBrandIconColor(string color)
To include relationships, append Include{Property}
at the end:
GetByBrandNameIncludeBrand
GetAllIncludeBrand
GetByActiveTrueIncludeBrand
Important: Include
statements must appear at the end of the method name.
- Support for
ThenInclude
- Automatic interface and controller generation (scaffolding-style)
- Sorting (
OrderBy
,OrderByDesc
) - Collection queries (
In
,Contains
)
This project is open-source. You can create issues, PRs, or suggest improvements. Contributions are welcome!
This project is under the MIT License.