Simple implementation of flexible parameterized sorting for IQuariable using IncrementalGenerator.
- Install library:
<ItemGroup>
<PackageReference Include="SourceGenLinq.Abstractions" Version="0.0.5" />
<PackageReference Include="SourceGenLinq.Generator" Version="0.0.5" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
- Define DTO class:
public class Tester
{
public Guid Id { get; set; }
public required string Name { get; set; }
public required string Comment { get; set; }
public required double Test { get; set; }
public QazWsx? QazWsx { get; set; }
}
public class QazWsx;
- Define sorter with generic attribute
SortQuariable
:
[SortQuariable<Tester>]
public static class MyTesterSorter;
- Use:
IQueryable<Tester> testers = new Tester[]
{
new()
{
Id = Guid.NewGuid(),
Name = "Test",
Comment = "Test",
Test = 22,
},
new()
{
Id = Guid.NewGuid(),
Name = "Test 2",
Comment = "Test 3",
Test = 100,
},
}.AsQueryable();
IQueryable<Tester> orderedTesters = testers.Sort([
new TesterSortInput()
{
Name = SortMode.Asc,
},
new TesterSortInput()
{
Comment = SortMode.Asc,
},
]);